首页 > 解决方案 > 有什么方法可以右键单击 Tkinter 列表框中的项目吗?

问题描述

我希望能够使用左键单击或右键单击在 Tkinter 列表框中选择一个项目。有什么方法可以绑定"<button-3>"到某种选择项目的函数,或者在将鼠标悬停在列表框上时从右键单击调用左键单击?

标签: pythonuser-interfacetkinter

解决方案


好吧,我想通了。

首先,使用绑定命令:

self.listBox.bind("<Button-3>", self.rightClick)

然后将 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))

推荐阅读