没有子菜单的示例
import sys
import tkinter as tk
def hello_handler():
pass
root = tk.Tk()
root.title(u"菜单")
# 创建一个菜单
menubar = tk.Menu(root)
menubar.add_command(label=u"单击我", command=hello_handler)
menubar.add_command(label=u"退出", command=root.quit)
# 将菜单添加到主窗口中
root.config(menu=menubar)
root.mainloop()
有子菜单(下拉菜单)的示例
import sys
import tkinter as tk
def hello_handler():
pass
root = tk.Tk()
root.title(u"演示下拉菜单的用法")
# 创建一个菜单
main_menu_bar = tk.Menu(root)
# 创建一个子菜单
filemenu = tk.Menu(main_menu_bar, tearoff=0)
filemenu.add_command(label=u"打开", command=hello_handler)
filemenu.add_command(label=u"保存", command=hello_handler)
filemenu.add_separator()
filemenu.add_command(label=u"退出", command=root.quit)
# 将子菜单加入到菜单条中
main_menu_bar.add_cascade(label=u"文件", menu=filemenu)
# 创建一个子菜单
editmenu = tk.Menu(main_menu_bar, tearoff=0)
editmenu.add_command(label=u"剪切", command=hello_handler)
editmenu.add_command(label=u"复制", command=hello_handler)
editmenu.add_command(label=u"粘贴", command=hello_handler)
# 将子菜单加入到菜单条中
main_menu_bar.add_cascade(label=u"编辑", menu=editmenu)
# 创建一个子菜单
helpmenu = tk.Menu(main_menu_bar, tearoff=0)
helpmenu.add_command(label=u"关于", command=hello_handler)
# 将子菜单加入到菜单条中
main_menu_bar.add_cascade(label=u"帮助", menu=helpmenu)
# 将菜单添加到主窗口中
root.config(menu=main_menu_bar)
root.mainloop()
思路:先把绿色做好,再把红色做好,最后把红色关联到绿色(级联)
tearoff Normally, a menu can be torn off, the first position (position 0) in the list of choices is occupied by the tear-off element, and the additional choices are added starting at position 1. If you set tearoff=0, the menu will not have a tear-off feature, and choices will be added starting at position 0.
tearoff的作用就是可以将每个菜单分离出去,单独形成一个子窗口。通常将tearoff设置为0。
REF
https://www.weixueyuan.net/a/800.html
http://www.wb86.com/post/326.html