如何在 tkinter 中将下拉菜单与顶级菜单结合起来
How to combine the drop-down menu with top-level menu in tkinter
我尝试制作一个带有菜单栏的简单文本编辑器,并创建了一个下拉菜单,但在菜单的末尾,我只想 "About" 没有任何下拉列表,但我做不到做吧。每次我想加的时候程序都是运行但是屏幕上没有显示,怎么办?
这是我的代码:
from tkinter import *
from tkinter import Menu, scrolledtext
from tkinter import messagebox
class GUI(Frame):
def __init__(self):
super().__init__()
self.pack()
self.drop_down_menu()
#self.toolbar()
self.text_area()
###GUI Drop-down menu
def drop_down_menu(self):
self.menu = Menu(root)
root.config(menu= self.menu)
self.submenu = Menu(self.menu)
self.menu.add_cascade(label = "File", menu = self.submenu)
self.submenu.add_command(label = "New", command = "")
self.submenu.add_command(label = "Open", command = "" )
self.submenu.add_command(label = "Open Recent", command = "")
self.submenu.add_separator()
self.submenu.add_command(label = "Save", command = "")
self.submenu.add_command(label = "Duplicate", command = "" )
self.submenu.add_command(label = "Rename", command = "")
self.submenu.add_command(label = "Move to", command = "")
self.submenu.add_separator()
self.submenu.add_command(label = "Exit", command = self.exit_function)
self.editmenu = Menu(self.menu)
self.menu.add_cascade(label = "Edit", menu = self.editmenu)
self.editmenu.add_command(label = "Undo", command = "")
self.editmenu.add_command(label = "Redo", command = "" )
self.editmenu.add_separator()
self.editmenu.add_command(label = "Cut", command = "")
self.editmenu.add_command(label = "Copy", command = "")
self.editmenu.add_command(label = "Paste", command = "")
self.editmenu.add_command(label = "Delete", command = "")
self.editmenu.add_separator()
self.editmenu.add_command(label = "Find", command = "")
self.viewmenu = Menu(self.menu)
self.menu.add_cascade(label = "View", menu = self.viewmenu)
self.viewmenu.add_command(label = "Zoom in", command = "")
self.viewmenu.add_command(label = "Zoom out", command = "" )
self.viewmenu.add_separator()
self.viewmenu.add_command(label = "Full Screen", command = "")
self.aboutmenu = Menu(self.menu)
self.aboutmenu.add_command(label="About", command="")
# def toolbar(self):
# self.toolbar = Frame(root, bg="#DCDCDC")
# self.toolbar.pack(side = TOP, fill = X)
#
#
# self.paragraph_button = Button(self.toolbar, text="Paragraph style", command="")
# self.paragraph_button.grid(row = 1, column = 0, columnspan= 3)
# self.font_button = Button(self.toolbar, text="Font-family", command="", height=1, width=3)
# self.font_button.grid(row=1, column=4)
# self.typeface_button = Button(self.toolbar, text="Typeface", command="", height=1, width=3)
# self.typeface_button.grid(row=1, column=5)
# self.font_size_button = Button(self.toolbar, text="Font size", command="", height=1, width=3)
# self.font_size_button.grid(row=1, column=6)
# self.font_color_button = Button(self.toolbar, text="Font color", command="", height=1, width=3)
# self.font_color_button.grid(row=1, column=7)
# self.bg_color_button = Button(self.toolbar, text="Background color", command="", height=1, width=3)
# self.bg_color_button.grid(row=1, column=8)
# self.bold_text_button = Button(self.toolbar, text="Bold text", command="", height=1, width=3)
# self.bold_text_button.grid(row=1, column=9)
# self.italicise_text_button = Button(self.toolbar, text="Italicise text", command="", height=1, width=3)
# self.italicise_text_button.grid(row=1, column=10)
# self.underline_text_button = Button(self.toolbar, text="Underline text", command="", height=1, width=3)
# self.underline_text_button.grid(row=1, column=11)
#
# self.left_align_button = Button(self.toolbar, text="Align to left", command="", height=1, width=3)
# self.left_align_button.grid(row=1, column=12)
# self.centere_text_button = Button(self.toolbar, text="Centere text", command="", height=1, width=3)
# self.centere_text_button.grid(row=1, column=13)
# self.right_align_button = Button(self.toolbar, text="Align to right", command="", height=1, width=3)
# self.centere_text_button.grid(row=1, column=14)
# self.justify_text_button = Button(self.toolbar, text="Justify text", command="", height=1, width=3)
# self.justify_text_button.grid(row=1, column=15)
#
# self.paragraph_spacing_button = Button(self.toolbar, text="Paragraph spacing", command="", height=1, width=3)
# self.paragraph_spacing_button.grid(row=1, column=16)
# self.paragraph_spacing_button = Button(self.toolbar, text="Paragraph spacing", command="", height=1, width=3)
# self.paragraph_spacing_button.grid(row=1, column=17)
def text_area(self):
textarea = scrolledtext.ScrolledText(root, width=100, height=50)
textarea.pack()
def exit_function(self):
if messagebox.askyesno("Close the window", "Do you want to close the window?", icon='warning'):
root.destroy()
else:
pass
def about(self):
messagebox.showinfo("About Greg's text editor", "This is the newest version of the Greg's text editor v.1.01")
root = Tk(className= " Text Editor")
app = GUI()
app.mainloop()
您将 "About" 命令附加到 self.aboutmenu
,但 self.aboutmenu
未附加到菜单栏。
您需要将 "About" 菜单添加到菜单栏。通常这位于 "Help" 菜单项下,而不是 "About"。大多数 UI 准则强烈反对您将命令直接放在菜单栏上,因为用户希望在单击菜单栏上的内容时看到菜单。
self.menu.add_cascade(label="Help", menu=self.aboutmenu)
如果您坚持将 "about" 命令直接放在菜单栏上而不是下拉菜单上,您可以试试。我不确定 Windows 或 Mac 是否允许,但它适用于 linux:
self.menu.add_command(label="About", command=about)
注意:tkinter 中对帮助菜单有特殊处理。来自规范 tcl/tk 文档:
Certain menus in a menubar will be treated specially. On the Macintosh, access to the special Application and Help menus is provided. On Windows, access to the Windows System menu in each window is provided. On X Windows, a special right-justified help menu may be provided if Motif menu compatibility is enabled. In all cases, these menus must be created with the command name of the menubar menu concatenated with the special name. So for a menubar named .menubar, on the Macintosh, the special menus would be .menubar.apple and .menubar.help; on Windows, the special menu would be .menubar.system; on X Windows, the help menu would be .menubar.help.
When Tk sees a .menubar.apple menu on the Macintosh, that menu's contents make up the first items of the Application menu whenever the window containing the menubar is in front. After all of the Tk-defined items, the menu will have a separator, followed by all standard Application menu items.
When Tk sees a Help menu on the Macintosh, the menu's contents are appended to the standard Help menu on the right of the user's menubar whenever the window's menubar is in front. The first items in the menu are provided by Mac OS X.
When Tk sees a System menu on Windows, its items are appended to the system menu that the menubar is attached to. This menu has an icon representing a spacebar, and can be invoked with the mouse or by typing Alt+Spacebar. Due to limitations in the Windows API, any font changes, colors, images, bitmaps, or tearoff images will not appear in the system menu.
When Tk sees a Help menu on X Windows and Motif menu compatibility is enabled the menu is moved to be last in the menubar and is right justified. Motif menu compatibility is enabled by setting the Tk option *Menu.useMotifHelp to true or by calling tk::classic::restore menu.
我尝试制作一个带有菜单栏的简单文本编辑器,并创建了一个下拉菜单,但在菜单的末尾,我只想 "About" 没有任何下拉列表,但我做不到做吧。每次我想加的时候程序都是运行但是屏幕上没有显示,怎么办?
这是我的代码:
from tkinter import *
from tkinter import Menu, scrolledtext
from tkinter import messagebox
class GUI(Frame):
def __init__(self):
super().__init__()
self.pack()
self.drop_down_menu()
#self.toolbar()
self.text_area()
###GUI Drop-down menu
def drop_down_menu(self):
self.menu = Menu(root)
root.config(menu= self.menu)
self.submenu = Menu(self.menu)
self.menu.add_cascade(label = "File", menu = self.submenu)
self.submenu.add_command(label = "New", command = "")
self.submenu.add_command(label = "Open", command = "" )
self.submenu.add_command(label = "Open Recent", command = "")
self.submenu.add_separator()
self.submenu.add_command(label = "Save", command = "")
self.submenu.add_command(label = "Duplicate", command = "" )
self.submenu.add_command(label = "Rename", command = "")
self.submenu.add_command(label = "Move to", command = "")
self.submenu.add_separator()
self.submenu.add_command(label = "Exit", command = self.exit_function)
self.editmenu = Menu(self.menu)
self.menu.add_cascade(label = "Edit", menu = self.editmenu)
self.editmenu.add_command(label = "Undo", command = "")
self.editmenu.add_command(label = "Redo", command = "" )
self.editmenu.add_separator()
self.editmenu.add_command(label = "Cut", command = "")
self.editmenu.add_command(label = "Copy", command = "")
self.editmenu.add_command(label = "Paste", command = "")
self.editmenu.add_command(label = "Delete", command = "")
self.editmenu.add_separator()
self.editmenu.add_command(label = "Find", command = "")
self.viewmenu = Menu(self.menu)
self.menu.add_cascade(label = "View", menu = self.viewmenu)
self.viewmenu.add_command(label = "Zoom in", command = "")
self.viewmenu.add_command(label = "Zoom out", command = "" )
self.viewmenu.add_separator()
self.viewmenu.add_command(label = "Full Screen", command = "")
self.aboutmenu = Menu(self.menu)
self.aboutmenu.add_command(label="About", command="")
# def toolbar(self):
# self.toolbar = Frame(root, bg="#DCDCDC")
# self.toolbar.pack(side = TOP, fill = X)
#
#
# self.paragraph_button = Button(self.toolbar, text="Paragraph style", command="")
# self.paragraph_button.grid(row = 1, column = 0, columnspan= 3)
# self.font_button = Button(self.toolbar, text="Font-family", command="", height=1, width=3)
# self.font_button.grid(row=1, column=4)
# self.typeface_button = Button(self.toolbar, text="Typeface", command="", height=1, width=3)
# self.typeface_button.grid(row=1, column=5)
# self.font_size_button = Button(self.toolbar, text="Font size", command="", height=1, width=3)
# self.font_size_button.grid(row=1, column=6)
# self.font_color_button = Button(self.toolbar, text="Font color", command="", height=1, width=3)
# self.font_color_button.grid(row=1, column=7)
# self.bg_color_button = Button(self.toolbar, text="Background color", command="", height=1, width=3)
# self.bg_color_button.grid(row=1, column=8)
# self.bold_text_button = Button(self.toolbar, text="Bold text", command="", height=1, width=3)
# self.bold_text_button.grid(row=1, column=9)
# self.italicise_text_button = Button(self.toolbar, text="Italicise text", command="", height=1, width=3)
# self.italicise_text_button.grid(row=1, column=10)
# self.underline_text_button = Button(self.toolbar, text="Underline text", command="", height=1, width=3)
# self.underline_text_button.grid(row=1, column=11)
#
# self.left_align_button = Button(self.toolbar, text="Align to left", command="", height=1, width=3)
# self.left_align_button.grid(row=1, column=12)
# self.centere_text_button = Button(self.toolbar, text="Centere text", command="", height=1, width=3)
# self.centere_text_button.grid(row=1, column=13)
# self.right_align_button = Button(self.toolbar, text="Align to right", command="", height=1, width=3)
# self.centere_text_button.grid(row=1, column=14)
# self.justify_text_button = Button(self.toolbar, text="Justify text", command="", height=1, width=3)
# self.justify_text_button.grid(row=1, column=15)
#
# self.paragraph_spacing_button = Button(self.toolbar, text="Paragraph spacing", command="", height=1, width=3)
# self.paragraph_spacing_button.grid(row=1, column=16)
# self.paragraph_spacing_button = Button(self.toolbar, text="Paragraph spacing", command="", height=1, width=3)
# self.paragraph_spacing_button.grid(row=1, column=17)
def text_area(self):
textarea = scrolledtext.ScrolledText(root, width=100, height=50)
textarea.pack()
def exit_function(self):
if messagebox.askyesno("Close the window", "Do you want to close the window?", icon='warning'):
root.destroy()
else:
pass
def about(self):
messagebox.showinfo("About Greg's text editor", "This is the newest version of the Greg's text editor v.1.01")
root = Tk(className= " Text Editor")
app = GUI()
app.mainloop()
您将 "About" 命令附加到 self.aboutmenu
,但 self.aboutmenu
未附加到菜单栏。
您需要将 "About" 菜单添加到菜单栏。通常这位于 "Help" 菜单项下,而不是 "About"。大多数 UI 准则强烈反对您将命令直接放在菜单栏上,因为用户希望在单击菜单栏上的内容时看到菜单。
self.menu.add_cascade(label="Help", menu=self.aboutmenu)
如果您坚持将 "about" 命令直接放在菜单栏上而不是下拉菜单上,您可以试试。我不确定 Windows 或 Mac 是否允许,但它适用于 linux:
self.menu.add_command(label="About", command=about)
注意:tkinter 中对帮助菜单有特殊处理。来自规范 tcl/tk 文档:
Certain menus in a menubar will be treated specially. On the Macintosh, access to the special Application and Help menus is provided. On Windows, access to the Windows System menu in each window is provided. On X Windows, a special right-justified help menu may be provided if Motif menu compatibility is enabled. In all cases, these menus must be created with the command name of the menubar menu concatenated with the special name. So for a menubar named .menubar, on the Macintosh, the special menus would be .menubar.apple and .menubar.help; on Windows, the special menu would be .menubar.system; on X Windows, the help menu would be .menubar.help. When Tk sees a .menubar.apple menu on the Macintosh, that menu's contents make up the first items of the Application menu whenever the window containing the menubar is in front. After all of the Tk-defined items, the menu will have a separator, followed by all standard Application menu items.
When Tk sees a Help menu on the Macintosh, the menu's contents are appended to the standard Help menu on the right of the user's menubar whenever the window's menubar is in front. The first items in the menu are provided by Mac OS X.
When Tk sees a System menu on Windows, its items are appended to the system menu that the menubar is attached to. This menu has an icon representing a spacebar, and can be invoked with the mouse or by typing Alt+Spacebar. Due to limitations in the Windows API, any font changes, colors, images, bitmaps, or tearoff images will not appear in the system menu.
When Tk sees a Help menu on X Windows and Motif menu compatibility is enabled the menu is moved to be last in the menubar and is right justified. Motif menu compatibility is enabled by setting the Tk option *Menu.useMotifHelp to true or by calling tk::classic::restore menu.