-1
我的代碼有3個ListBox。我在每個列表中都附加了一個ListBoxSelect,所以當我單擊一個列表框時,它會選擇另一個列表框的同一行。Tkinter listbox << ListBoxSelect >>無法正常工作
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()
打開第一次點擊(無論在什麼列表框上)它的工作原理是完美的。第二次點擊時,它只是在第一個列表框上工作,而另外兩個則沒有任何反應。
爲什麼這不是第二次點擊其他列表框?
謝謝。
嘗試打印出什麼'NUM [0]'是在'immediately'。我的猜測是,這不是你所期望的。應用基本的調試技巧:打印中間變量以驗證您的假設。 –