将事件/命令绑定到列表框中的项目(使用滚动条)

时间:2018-01-22 19:33:30

标签: python tkinter listbox

我正在创建一个程序,可以在其中创建任意数量的用户,并且要选择一个,您必须单击其名称,该名称包含在可滚动的列表框中。 有没有办法(绑定或其他方式)在点击列表框中的不同项目时执行命令?

1 个答案:

答案 0 :(得分:0)

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)

我在帖子Getting a callback when a Tkinter Listbox selection is changed?的评论中提到了这一点,其中包含上述答案。