我的代码有3个ListBox。我在每个上面附加了一个ListBoxSelect,所以当我点击一个listBox时,它会选择另一个列表框的同一行。
def __init__(self, master, lists):
Frame.__init__(self, master)
self.lists = []
for column,size in lists:
frame = Frame(self); frame.pack(side=LEFT, expand=YES, fill=BOTH)
Label(frame, text=column, borderwidth=1, relief=RAISED).pack(fill=X)
lb = Listbox(frame, width=size, borderwidth=1, selectborderwidth=1, relief=FLAT, export=FALSE)
lb.pack(expand=YES, fill=BOTH)
lb.bind('<<ListboxSelect>>',self.immediately)
lb.bind("<MouseWheel>", self._on_mousewheel)
self.lists.append(lb)
def immediately(self, e):
#Find the element
for elem in self.lists:
num = elem.curselection()
if len(num) == 1:
break
#Unselect every element
for elem in self.lists:
elem.selection_clear(0,elem.size())
#Select elements in the same row
for i in range(self.lists[0].size()):
for elem in self.lists:
elem.selection_set(num[0])
if __name__ == '__main__':
window = Tk()
window.title("Account Manager")
window.resizable(0,0)
window.geometry('+100+200')
accountList = MultiListbox(window, (('Website', 20), ('Account Name', 20), ('Password', 20)))
accountList.pack(expand=YES,fill=BOTH)
accountList.loadListFromFile()
window.mainloop()
在第一次点击(无论在哪个列表框上),它完美无缺。在第二次单击时,它只适用于第一个listBox,另外两个没有任何反应。
为什么第二次点击另一个列表框?
感谢。
答案 0 :(得分:2)
这是因为您始终在immediately(...)
函数的第一个for循环中的第一个列表框中找到所选项。使用e.widget
(列表框触发函数)查找所选项目:
def immediately(self, e):
num = e.widget.curselection()
for lb in self.lists:
if lb != e.widget:
lb.selection_clear(0, lb.size())
lb.selection_set(num[0])