我想限制弹出菜单可以触发的区域
我当前的代码允许在用户右键单击时在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()
我希望仅在用户右键单击标签“右键单击以显示菜单”时触发弹出菜单