[英]How to bind the pop up menu to a label in Tkinter
我想限制可以触发弹出菜单的区域
我当前的代码允许在用户右键单击时在tkinter窗口中的任何位置触发弹出菜单
from tkinter import *
root = Tk()
w = Label(root, text="Right-click to display menu", width=40, height=20)
w.pack()
popup = Menu(root, tearoff=0)
popup.add_command(label="Next") # , command=next) etc...
popup.add_command(label="Previous")
popup.add_separator()
popup.add_command(label="Home")
def do_popup(event):
try:
popup.tk_popup(event.x_root, event.y_root, 0)
finally:
popup.grab_release()
w.bind("<Button-3>", do_popup)
b = Button(root, text="Quit", command=root.destroy)
b.pack()
mainloop()
我希望仅在用户右键单击标签“右键单击以显示菜单”时触发弹出菜单
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.