bfo*_*ops 42 python events tkinter
在Tkinter 中有多种方法可以获得回调Text
或者Entry
更改小部件,但是我没有找到一个用于回调的方法Listbox
(我找不到的大部分事件文档是旧的还是不完整的).有没有办法为此生成一个事件?
Pie*_*ert 62
def onselect(evt):
# Note here that Tkinter passes an event object to onselect()
w = evt.widget
index = int(w.curselection()[0])
value = w.get(index)
print 'You selected item %d: "%s"' % (index, value)
lb = Listbox(frame, name='lb')
lb.bind('<<ListboxSelect>>', onselect)
Run Code Online (Sandbox Code Playgroud)
Bry*_*ley 47
你可以绑定到:
<<ListboxSelect>>
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,我需要使用 selectmode=MULTIPLE 获取列表框中最后选定的项目。如果其他人也遇到同样的问题,这就是我所做的:
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()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
44397 次 |
最近记录: |