Reputation: 101
I try to make a simple text editor with a menubar and have created a drop-down menu but at the end of the menu I want just simply "About" without any drop-down list and I can not do it. Every time I want to add it the program is running but it doesn't appear on the screen, how can I do it?
Here is my code:
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()
Upvotes: 0
Views: 2155
Reputation: 385970
You're attaching the "About" command to self.aboutmenu
, but self.aboutmenu
isn't attached to the menubar.
You need to add the "About" menu to the menubar. Typically this goes under a "Help" menu item rather than "About". Most UI guidelines strongly discourage you from putting commands directly on the menubar, since users expect to see a menu when they click something on the menubar.
self.menu.add_cascade(label="Help", menu=self.aboutmenu)
If you insist on putting the "about" command directly on the menubar rather than on a dropdown, you can try. I'm not sure if Windows or the Mac will allow it, but it works on linux:
self.menu.add_command(label="About", command=about)
Note: There is special handling of help menus in tkinter. From the canonical tcl/tk documentation:
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.
Upvotes: 1