Tkinter Menu Tutorial
A menu is usually designed in a window. A menu is a drop-down form in which a list of menus can be designed. The method to create a menu is Menu( ).
The syntax format of Menu is as follows.
Menu(master, options, ...)
The first parameter of the Menu( ) method is the parent object, indicating which parent object this menu will be built in. The following are the other commonly used options parameters within the Menu( ) method.
(1) activebackground
: the background color when the cursor is moved to this menu list.
(2) activeborderwidth
: the thickness of its outer border when selected by the mouse, default is 1.
(3) activeforeground
: the foreground color when the cursor is moved to this menu list.
(4) bd
: the thickness of the outer border of all menu lists, the default is 1.
(5) bg
: the background color of the menu list when it is not selected.
(6) cursor
: the appearance of the mouse cursor on the list when the menu is separated.
(7) disabledforeground
: the color of the menu list when it is DISABLED.
(8) font
: the glyph of the menu list text.
(9) fg
: the foreground color of the menu list when it is not selected.
(10) image
: icon of the menu.
(11) tearoff
: menu above the separator line, which is a dashed line, there is a separator line when the tearof f value is True or 1, then the menu list is placed from position 1, while the menu can be separated, the separation is to open the menu after clicking the separator line. If tearof f is set to False or 0, the separator line will not be displayed, that is, the menu cannot be separated, but the menu list will be stored from position 0.
The following are other related methods.
(1) add_cascade()
: Create a hierarchical menu, and at the same time let this child function list to establish a link with the parent menu.
(2) add_command()
: Add a list of menus.
(3) add_separator()
: add a separator for the menu list.
Example#1
Create the top menu list, click "Hello!" and the Welcome to apidemos.com
dialog box will appear, click "Exit!" and the program will end.
from tkinter import *
from tkinter import messagebox
def hello():
messagebox.showinfo("Hello","Welcome to apidemos.com")
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
# Create top-level menu
menubar = Menu(root)
menubar.add_command(label="Hello!",command=hello)
menubar.add_command(label="Exit!",command=root.destroy)
root.config(menu=menubar) # Show menu objects
root.mainloop()
Output:
The above design concept is to first create the menubar object in line 13, and then create the Hello! and Exit! command lists on the menubar in lines 14 and 15 respectively.
Although the above program can be executed, but this is not a formal way of menu design, the formal menu is to create the menu category at the top first, and then create the related submenu list in each menu category, these submenu list is displayed with a drop-down form.
Example#2
Create a File menu, then create a drop-down list of commands within this menu.
from tkinter import *
from tkinter import messagebox
def newFile():
messagebox.showinfo("New File-apidemos","New Document-apidemos")
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
menubar = Menu(root) # Create top-level menu
# Create a menu category object, and name this menu category as File
filemenu = Menu(menubar)
menubar.add_cascade(label="File File-apidemos",menu=filemenu)
# Create a menu list within the File menu
filemenu.add_command(label="New File New File-apidemos",command=newFile)
filemenu.add_command(label="Exit Exit-apidemos",command=root.destroy)
root.config(menu=menubar) # Show menu objects
root.mainloop()
Output:
The key to establishing the File menu above is lines 14 and 15, and filemenu is the object of the File menu. Lines 17 and 18 use the filemenu object to create a list of New File and Exit! commands within the File menu.