我有一个与listbox绑定的ListboxSelect事件。我正在使用selection_set来选择列表框中的项目,但绑定的函数没有运行。当我使用selection_set在列表框中选择项目时,如何使函数运行以及当单击它时?
import tkinter as tk
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.lb = tk.Listbox(self)
for x in range(20):
self.lb.insert("end", x)
self.lb.bind("<<ListboxSelect>>", self.OnSelect)
self.lb.pack(side="top", fill="both", expand=True)
def OnSelect(self, event):
print(event.widget.get(event.widget.curselection()[0]))
self.lb.selection_set(10)
if __name__ == "__main__":
app = SampleApp()
app.mainloop()