import tkinter
win = tkinter.Tk()
win.title("Menu顶层菜单栏")
win.geometry("800x600+600+100")
#菜单条
menubar =tkinter.Menu(win)
win.config(menu=menubar)
def func():
print("lalaa")
#创建一个菜单选项
menu1=tkinter.Menu(menubar,tearoff=False)
#给菜单选项,添加菜单内容
for item in ["Python","C","C++","OC","Swift","C#","shell",
"java","JS","PHP","汇编","NodeJs","退出"]:
if item=="退出":
menu1.add_separator()#添加一个分割线
menu1.add_command(label=item,command=lambda :win.quit())#lambda:win.quit==win.quit
else:
menu1.add_command(label=item,command=func)
#向菜单条上添加菜单
menubar.add_cascade(label="语言",menu=menu1)
menu2=tkinter.Menu(menubar,tearoff=False)
menu2.add_command(label="red")
menu2.add_command(label="blue")
menubar.add_cascade(label="颜色",menu=menu2)
win.mainloop()
转载于:https://www.cnblogs.com/zlong123/p/10498126.html