我尝试用菜单栏制作一个简单的文本编辑器,并创建了一个下拉菜单,但在菜单末尾我只想简单地“关于”,没有任何下拉列表,我做不到。每次我想添加它时程序正在运行但它没有出现在屏幕上,我该怎么做?
这是我的代码:
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()