Я пытаюсь сделать простой текстовый редактор с строкой меню и создал раскрывающееся меню, но в конце меню я хочу просто «О программе» без раскрывающегося списка, и я не могу этого сделать. Каждый раз, когда я хочу добавить его, программа работает, но не отображается на экране, как мне это сделать?
Вот мой код:
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()
Вы присоединяете команду «О программе» к self.aboutmenu
, но self.aboutmenu
не прикреплен к строке меню.
Вам необходимо добавить меню «О программе» в строку меню. Обычно это происходит в пункте меню «Справка», а не в «О программе». Большинство руководств по пользовательскому интерфейсу настоятельно не рекомендуют размещать команды непосредственно в строке меню, поскольку пользователи ожидают увидеть меню, когда они щелкнут что-либо в строке меню.
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.
@GrzegorzSpytek: Я не понимаю вашего комментария. Добавление about
в качестве команды в строку меню выполняется точно так же, как вы добавляете команды для всех других меню. Для menu=
нет add_command
, потому что вы добавляете команда, а не меню.
Звучит вполне справедливо. Я добавлю раздел помощи. Но в любом случае я не решил это меню верхнего уровня, так как в методе add_command нет такого параметра, как «menu =», не могли бы вы показать мне, как бы вы это сделали.