python - 更改 tkinter 命令的右键单击事件
问题描述
我想检测 tkinter Menu 命令上的右键单击事件。考虑下面的代码。
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
menu_button = ttk.Menubutton(root, text="MENU")
menu_button.grid()
m = tk.Menu(menu_button, tearoff=False, activeborderwidth=0)
menu_button["menu"] = m # To avoid garbage collection
m.add_command(label="an option", command=lambda: print("option1"))
m.add_command(label="another option", command=lambda: print("option2"))
root.mainloop()
当我单击an option
oranother option
时,命令按预期调用。但我想做的是捕捉右键单击事件。谁能知道我该如何检测它?
解决方案
使用button.bind("<Button-3>", event)
. 考虑这段代码:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
button = tk.Button(root, text='right click this')
button.pack()
button.bind("<Button-3>", lambda e: print('You right clicked'))
root.mainloop()
推荐阅读
- python - 使用 unittest.mock.patch 和 requests 捕获异常的问题
- c++ - C++17 中的折叠表达式
- vb.net - 如何在 vb 中修复此表达式和值错误?
- java - 在 Python 中压缩并在 Java 中解压缩以进行数据传输
- android - RecyclerView 在 Android Studio 3.5.3 中不显示项目
- javascript - 100% 的 Jest 代码覆盖率,在 try-catch 和 throw 结构中存在未知错误
- python - 打印从 1 到 100 的所有数字列表,跳过可被 3 或 5 整除的数字
- php - 尝试使用 destroy 函数执行一些删除操作时说 Error:Missing required parameters for Route Laravel 6.0 我该如何修复它
- office365 - /mailFolders/inbox/messageRules HTTP 503
- python - 狮身人面像错误;缺少 PocketSphinx 模块:确保正确设置了 PocketSphinx