lastselectionList = []
def onselect(evt):
# Note here that Tkinter passes an event object to onselect()
global lastselectionList
w = evt.widget
if lastselectionList: #if not empty
#compare last selectionlist with new list and extract the difference
changedSelection = set(lastselectionList).symmetric_difference(set(w.curselection()))
lastselectionList = w.curselection()
else:
#if empty, assign current selection
lastselectionList = w.curselection()
changedSelection = w.curselection()
#changedSelection should always be a set with only one entry, therefore we can convert it to a lst and extract first entry
index = int(list(changedSelection)[0])
value = w.get(index)
tkinter.messagebox.showinfo("You selected ", value)
listbox = tk.Listbox(frame,selectmode=tk.MULTIPLE)
listbox.bind('<<ListboxSelect>>', onselect)
listbox.pack()
我遇到了一个问题,需要在selectmode=MULTIPLE的列表框中获取最后一个选中的项。如果其他人也有同样的问题,以下是我所做的:
您可以绑定到:
相关问题 更多 >
编程相关推荐