我试图用菜单栏制作一个简单的文本编辑器,并创建了一个下拉菜单,但是在菜单末尾,我只想简单地“ 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()
答案 0 :(得分:1)
您正在将“关于”命令附加到self.aboutmenu
,但是self.aboutmenu
未附加到菜单栏。
您需要在菜单栏中添加“关于”菜单。通常,它位于“帮助”菜单项下,而不是“关于”下。大多数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文档中:
将对菜单栏中的某些菜单进行特殊处理。在Macintosh上,提供对特殊的“应用程序”和“帮助”菜单的访问。在Windows上,提供对每个窗口中Windows系统菜单的访问。在X Windows上,如果启用了Motif菜单兼容性,则可能会提供一个特殊的右对齐帮助菜单。在所有情况下,必须使用带有特殊名称的菜单栏菜单的命令名称来创建这些菜单。因此,对于Macintosh上名为.menubar的菜单栏,特殊菜单为.menubar.apple和.menubar.help;在Windows上,特殊菜单为.menubar.system;。在X Windows上,帮助菜单为.menubar.help。 当Tk在Macintosh上看到.menubar.apple菜单时,只要包含菜单栏的窗口在最前面,该菜单的内容就构成了“应用程序”菜单的第一项。在所有Tk定义的项目之后,菜单将带有分隔符,然后是所有标准的“应用程序”菜单项。
当Tk在Macintosh上看到“帮助”菜单时,只要窗口的菜单栏位于前面,该菜单的内容就会附加到用户菜单栏右侧的标准“帮助”菜单中。菜单中的第一项是Mac OS X提供的。
当Tk在Windows上看到“系统”菜单时,其项目将附加到菜单栏所附的系统菜单。此菜单有一个表示空格键的图标,可以用鼠标或通过键入Alt +空格键来调用。由于Windows API的限制,任何字体更改,颜色,图像,位图或撕裂图像都不会出现在系统菜单中。
当Tk在X Windows上看到“帮助”菜单并且启用了Motif菜单兼容性时,该菜单将移动到菜单栏中的最后一个并且右对齐。通过将Tk选项* Menu.useMotifHelp设置为true或调用tk :: classic :: restore菜单,可以启用Motif菜单兼容性。