一个关于tkinter弹出菜单的问题
在主界面上右键弹出一个菜单选项理解了 。现在想在弹出的不同的Label窗口上,右键再弹出菜单 不知道事件怎么绑定了
程序如下,请大佬解惑
from tkinter import *
tool = Tk()
def op():
pass
def sv():
pss
def callback(event):
menu = Menu(这里面不会写)
menu.add_command(label = '打开',command = op)
menu.add_command(label = '保存',command = sv)
(下面不会写)
l1 = Label(tool,text='右键点我')
l2 = Label(tool,text ='也右键我')
l1.bind('<Button-3>',callback)
l1.pack()
l2.bind('<Button-3>',callback)
l2.pack()
mainloop()
本帖最后由 qq1151985918 于 2021-1-30 18:51 编辑
from tkinter import *
tool = Tk()
tool.geometry("200x100")
def op():
pass
def sv():
pass
def callback(event):
menu = Menu(tool ,tearoff=0)
menu.add_command(label = '打开',command = op)
menu.add_separator()
menu.add_command(label = '保存',command = sv)
menu.post(event.x_root, event.y_root)
l1 = Label(tool,text='右键点我')
l2 = Label(tool,text ='也右键我')
l1.bind('<Button-3>',callback)
l1.pack()
l2.bind('<Button-3>',callback)
l2.pack()
mainloop()
这样即可,参考代码:
from tkinter import *
tool = Tk()
def op():
print('op')
def sv():
print('sv')
def callback(event):
menu.post(event.x_root, event.y_root)
menu = Menu(tool,tearoff=False)
menu.add_command(label = '打开',command = op)
menu.add_command(label = '保存',command = sv)
l1 = Label(tool,text='右键点我')
l2 = Label(tool,text ='也右键我')
l1.bind('<Button-3>',callback)
l1.pack()
l2.bind('<Button-3>',callback)
l2.pack()
mainloop() 谢谢大佬
页:
[1]