Jad*_*win -1 python user-interface tkinter
我希望能够使用左键单击或右键单击在 Tkinter 列表框中选择一个项目。有什么方法可以绑定"<button-3>"到某种选择项目的函数,或者在将鼠标悬停在列表框上时从右键单击调用左键单击?
好吧,我想通了。
首先,使用绑定命令:
self.listBox.bind("<Button-3>", self.rightClick)
Run Code Online (Sandbox Code Playgroud)
然后使用 selection_clear 和 selection_set 与最近的函数来获取光标所在的索引,然后激活它:
def rightClick(self,event):
self.listBox.selection_clear(0,tk.END)
self.listBox.selection_set(self.listBox.nearest(event.y))
self.listBox.activate(self.listBox.nearest(event.y))
Run Code Online (Sandbox Code Playgroud)