Mune
窗口
窗口上部菜单条
实现菜单项可选择
实现分割线
实现点击触发事件
import tkinter
window = tkinter.Tk()
window.title("Menu")
window.geometry("400x200+100+20")
def func():
print("打印内容")
print(menul.value)
menubar = tkinter.Menu(window)
window.config(menu=menubar)
menul = tkinter.Menu(menubar,tearoff=False)
mv = tkinter.StringVar()
for item in ["java","python","C","退出"]:
if item == "退出":
menul.add_separator()
menul.add_command(label=item,command=window.quit)
else:
menul.add_command(label=item,command=func)
menubar.add_cascade(label="语言",menu=menul)
menul2 = tkinter.Menu(menubar,tearoff=False)
menul2.add_command(label="red",command=func)
menul2.add_command(label="black",command=func)
menubar.add_cascade(label="颜色",menu=menul2)
window.mainloop()
窗口
右击鼠标显示菜单条
实现菜单项可选择
实现分割线
实现点击触发事件
import tkinter
window = tkinter.Tk()
window.title("鼠标右键菜单")
window.geometry("400x200+100+20")
menubar = tkinter.Menu(window)
menu = tkinter.Menu(menubar,tearoff=False)
for item in ["java","python","C","退出"]:
menu.add_command(label=item)
menubar.add_cascade(label="语言",menu=menu)
def showMenuMethod(event):
menubar.post(event.x_root,event.y_root)
window.bind("",showMenuMethod)
window.mainloop()