python - 有什么方法可以右键单击 Tkinter 列表框中的项目吗?
问题描述
我希望能够使用左键单击或右键单击在 Tkinter 列表框中选择一个项目。有什么方法可以绑定"<button-3>"
到某种选择项目的函数,或者在将鼠标悬停在列表框上时从右键单击调用左键单击?
解决方案
好吧,我想通了。
首先,使用绑定命令:
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))
推荐阅读
- python - 在 Windows Server 上运行时出现“unicode 错误”,但在我的电脑上运行正常
- c# - 有没有办法在 Visual Studio 2017 中转到下一个绿色或黄色边距区域?
- java - 通过 docker-compose 将 JAVA_OPTS 传递给 Spring Boot 应用程序
- android - 使用 Android Studio 安装 Flutter
- reactjs - 在 React 中使用 this.state 中的值的语法
- azure-devops - ARM错误的VSTS管道部署:找不到与模板文件模式匹配的任何文件
- d3.js - 使用 D3 的可拖动交互式图形元素
- jenkins - 在 Jenkins Scripted Pipeline 中跨阶段设置和引用变量
- raku - 约束多重及其用于选择它们的用途
- javascript - Hc 粘性插件 IE11 问题 - 导航栏没有全宽