【问题标题】:Change right click event of tkinter command更改 tkinter 命令的右键单击事件
【发布时间】:2021-03-29 14:54:24
【问题描述】:

我想检测 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 optionanother option 时,会按预期调用命令。但我想做的是捕捉右键单击事件。有谁知道我该如何检测它?

【问题讨论】:

  • 您的意思是,当您右键单击菜单时,会运行一个命令?
  • 如果有帮助,您可以这样做menu_button.bind('<Button-3>', lambda e: print("option0"))

标签: python tkinter tkinter-menu


【解决方案1】:

使用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()

【讨论】:

    猜你喜欢
    • 2016-08-05
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2021-04-12
    • 2013-06-28
    • 2017-05-30
    • 1970-01-01
    相关资源
    最近更新 更多