Skip to content Skip to sidebar Skip to footer

Selecting Values Simultaneously From Different Tkinter Listbox Widgets In Python 3.5

I want my program to work in a way that: when I click an element from either of the 3 listboxes to select the adjacent values (example: if i select 0 in the first listbox, it shou

Solution 1:

The first thing you want to do is bind to the <<ListboxSelect>> event to detect when an option is selected in one of your list-boxes:

def __init__(self, master):

    # ...

    self.lista1=Listbox(self,yscrollcommand=self.yscroll1,exportselection=0)
    self.lista1.bind('<<ListboxSelect>>', self.on_select1)
    self.lista1.grid(row=3,column=1)

    self.lista2=Listbox(self, yscrollcommand=self.yscroll2,exportselection=0)
    self.lista2.bind('<<ListboxSelect>>', self.on_select2)
    self.lista2.grid(row=3, column=2)

    self.lista3=Listbox(self, yscrollcommand=self.yscroll3,exportselection=0)
    self.lista3.bind('<<ListboxSelect>>', self.on_select3)
    self.lista3.grid(row=3, column=3)

     # ...

When an option is selected, you want to select the corresponding option in the other list-boxes:

def on_select1(self, event):
    # Get index of selected option.
    index = self.lista1.curselection()[0]
    # Select corresponding options in other list-boxes.
    self.select_others(index, self.lista2, self.lista3)

def on_select2(self, event):
    # Get index of selected option.
    index = self.lista2.curselection()[0]
    # Select corresponding options in other list-boxes.
    self.select_others(index, self.lista1, self.lista3)

def on_select3(self, event):
    # Get index of selected option.
    index = self.lista3.curselection()[0]
    # Select corresponding options in other list-boxes.
    self.select_others(index, self.lista1, self.lista2)

def select_others(self, index, *others):
    for listbox in others:
        # The list-box's selection must be cleared to prevent the previous selection from remaining.
        listbox.selection_clear(0, listbox.size() - 1)
        listbox.selection_set(index)

Finally, the self.pack() at the end of the face3 class is misplaced.

class face3(Frame):

    # ...

    self.pack()

You should pack lf instead:

root = Tk()

marco = Frame(root, padx=100, pady=50)
marco.pack()

lf = fase3(marco)
lf.pack()

root.mainloop()

Post a Comment for "Selecting Values Simultaneously From Different Tkinter Listbox Widgets In Python 3.5"