Manish Pushpam
Manish Pushpam

Reputation: 194

How to select item from two different Listbox on Single click

I am able to achieve this on Double Click.

Here is my sample code.

Sample Image On Signle Click

enter image description here

Sample Image on Double Click

enter image description here

Some Explanation of my code:

  1. I have created two listbox : list_box_one, list_box_two
  2. And scrolling both widget at a single time.
  3. And I want to select item from both listbox on single click.
import tkinter as tk


class App:
    def __init__(self):
        self.root = tk.Tk()

        self.scroll = tk.Scrollbar(orient="vertical", command=self.Onscroll)
        self.scroll.pack(side="right", fill="y")

        self.list_box_one = tk.Listbox(self.root,
                                       borderwidth=0,
                                       yscrollcommand=self.scroll.set)
        self.list_box_one.pack(side="left", fill="both", expand=True)
        self.list_box_one.config(exportselection=False,
                                 highlightthickness=0,
                                 selectbackground='red')
        self.list_box_one.bind("<MouseWheel>", self.OnMouseWheel)
        self.list_box_one.bind('<Double-Button-1>', self.fileSelection)

        self.list_box_two = tk.Listbox(self.root,
                                       borderwidth=0,
                                       yscrollcommand=self.scroll.set)
        self.list_box_two.pack(side="left", fill="both", expand=True)
        self.list_box_two.config(exportselection=False,
                                 highlightthickness=0,
                                 selectbackground='red')
        self.list_box_two.bind("<MouseWheel>", self.OnMouseWheel)
        self.list_box_two.bind('<Double-Button-1>', self.fileSelection)

        for i in range(100):
            self.list_box_one.insert("end", "item %s" % i)
            self.list_box_two.insert("end", "item %s" % i)
        self.root.mainloop()

    def Onscroll(self, *args):
        self.list_box_one.yview(*args)
        self.list_box_two.yview(*args)

    def OnMouseWheel(self, event):
        self.list_box_one.yview_scroll(-1 * int(event.delta / 120), "units")
        self.list_box_two.yview_scroll(-1 * int(event.delta / 120), "units")
        return "break"

    def fileSelection(self, event):
        widget = event.widget
        index = widget.curselection()[0]  # Index Of Selected Item

        self.list_box_two.selection_clear(0, 'end')  # Clear Old Selected Item
        self.list_box_one.selection_clear(0, 'end')  # Clear Old Selected Item
        self.list_box_one.selection_set(index)  # Selecting Current Item
        self.list_box_two.selection_set(index)  # Selecting Current Item


app = App()

Upvotes: 0

Views: 112

Answers (1)

acw1668
acw1668

Reputation: 46678

You can replace <Double-Button-1> by <<ListboxSelect>> in the two bind(...).

Also add the following two lines at the end of fileSelection() function:

self.list_box_one.see(index)
self.list_box_two.see(index)

Upvotes: 1

Related Questions