"""开发记事本软件的菜单"""
from tkinter.filedialog import *
from tkinter.colorchooser import *
from tkinter import Menu, Tk, Text, Frame
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.textpad = None # 表示Text文本框对象
self.pack()
self.create_widget()
def create_widget(self):
# 创建主菜单
menubar = Menu(root)
# 创建子菜单
menu_file = Menu(menubar)
menu_edit = Menu(menubar)
menu_help = Menu(menubar)
# 将子菜单加入到主菜单栏
menubar.add_cascade(label="文件(F)", menu=menu_file)
menubar.add_cascade(label="编辑(E)", menu=menu_edit)
menubar.add_cascade(label="帮助(H)", menu=menu_help)
# 添加菜单项
menu_file.add_command(label="新建", accelerator="ctrl+n", command=self.func)
menu_file.add_command(label="打开", accelerator="ctrl+o", command=self.func)
menu_file.add_command(label="保存", accelerator="ctrl+s", command=self.func)
menu_file.add_separator() # 添加分割线
menu_file.add_command(label="退出", accelerator="ctrl+q", command=self.func)
# 将主菜单栏加到根窗口
root["menu"] = menubar
# 文本编辑区
self.textpad = Text(root, width=50, height=30)
self.textpad.pack()
def func(self):
pass
if __name__ == '__main__':
root = Tk()
root.geometry("400x100")
root.title("花事本")
app = Application(master=root)
root.mainloop()
标签:code,14,menu,self,add,command,root,menubar
From: https://www.cnblogs.com/aihonghua/p/18169985