Selecting values simultaneously from different Tkinter Listbox widgets in Python 3.5 -
i want program work in way that:
- when click element either of 3 listboxes select adjacent values (example: if select 0 in first listbox, should select value "hello" in second listbox , "world" in third listbox
thanks this question/answer managed make them move simultaneously
the code following:
from tkinter import * class fase3(frame): def __init__(self, master): frame.__init__(self,master) self.master=master self.frame=frame(self.master) self.txtm=label(self, text="example") self.txtm.grid(row=1, columnspan=5) self.txt1=label(self, text="1") self.txt1.grid(row=2, column=1) self.txt2=label(self, text="2") self.txt2.grid(row=2,column=2) self.txt3=label(self, text="3") self.txt3.grid(row=2,column=3) self.scrlb=scrollbar(self,orient="vertical") self.lista1=listbox(self,yscrollcommand=self.yscroll1,exportselection=0) self.lista1.grid(row=3,column=1) self.lista2=listbox(self, yscrollcommand=self.yscroll2,exportselection=0) self.lista2.grid(row=3, column=2) self.lista3=listbox(self, yscrollcommand=self.yscroll3,exportselection=0) self.lista3.grid(row=3, column=3) #self.scrlb.config(command=self.yview) #self.scrlb.grid(row=3, column=4,rowspan=1) #i chose not place scrollbar because involved frames , makes code longer in range(3): self.lista1.insert("end",i) self.lista2.insert("end","hello") self.lista2.insert("end","pls") self.lista2.insert("end","thanks") self.lista3.insert("end","world") self.lista3.insert("end","help") self.lista3.insert("end","alot") def yscroll1(self, *args): if self.lista2.yview()!=self.lista1.yview()!=self.lista3.yview(): self.lista2.yview_moveto(args[0]) self.lista3.yview_moveto(args[0]) self.scrlb.set(*args) def yscroll2(self, *args): if self.lista1.yview()!=self.lista2.yview()!=self.lista1.yview(): self.lista1.yview_moveto(args[0]) self.lista3.yview_moveto(args[0]) self.scrlb.set(*args) def yscroll3(self, *args): if self.lista2.yview()!=self.lista3.yview()!=self.lista1.yview(): self.lista1.yview_moveto(args[0]) self.lista2.yview_moveto(args[0]) self.scrlb.set(*args) def yview(self, *args): self.lista1.yview(*args) self.lista2.yview(*args) self.lista3.yview(*args) self.pack() root = tk() marco= frame(root, padx=100, pady=50) marco.pack() lf = fase3(marco) root.mainloop()
part of problem is, have function bound every listbox, when add exportselection=0
, selects single value, so, example user can select ("0","hello","alot"), purposes make no sense @ (insertion database)
i haven't made process in this, if possible them move simultaneously, should somehow possible them select adjacent values.
you should able copy paste , run on python without problem, made in python 3.
the first thing want bind <<listboxselect>>
event detect when option selected in 1 of 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 option selected, want select corresponding option in other list-boxes:
def on_select1(self, event): # 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): # 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): # 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): listbox in others: # list-box's selection must cleared prevent previous selection remaining. listbox.selection_clear(0, listbox.size() - 1) listbox.selection_set(index)
finally, self.pack()
@ end of face3
class 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()
Comments
Post a Comment