Python Tkinter Menu
Creating Menus in Python Applications by tkinter library
In this tutorial, we will show you how to create menus in the Python applications by tkitner library.
First, the examples will explain how to add a menu. Then we will add menu items as a user pulls/clicks on the menu.
Later, we will also show how to add sub menus inside the main menu.
Actions as you click on the menu item will also be covered in the examples below.
w = Menu(top, options)
Let us start with a simple example of creating a main menu with a single item.
We will create “File” main menu and then it will contain one item “Open”.
As you open and click on the Open menu item, it closes the window.
Have a look at the code and output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import tkinter as tk from tkinter import Menu # window with a menu bar win = tk.Tk() win.geometry("450x350") win.title('Creating a Menu') # create a menubar main_menu = Menu(win) filemenu = Menu(main_menu, tearoff=0) # Add title of the main menu main_menu.add_cascade( label="File", menu=filemenu ) # Adding first menu item filemenu.add_command( label='Open', command=win.destroy ) win.config(menu=main_menu) win.mainloop() |
Output:
Let us move ahead and add three items to the “File” menu:
- New
- Open
- Save
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import tkinter as tk from tkinter import Menu # window with a menu bar win = tk.Tk() win.geometry("450x350") win.title('Creating a Menu') # create a menubar main_menu = Menu(win) filemenu = Menu(main_menu, tearoff=0) # Add title of the main menu main_menu.add_cascade( label="File", menu=filemenu ) #Adding menu items to the File menu filemenu.add_command(label="New") filemenu.add_command(label="Open") filemenu.add_command(label="Save") win.config(menu=main_menu) win.mainloop() |
Output:
We will add a separator between each menu item (New, Open, and Save) for the demo only.
To add a separator, use the add_separator() method of the menu widget.
All items simply close the window as you click on any:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import tkinter as tk from tkinter import Menu # window with a menu bar win = tk.Tk() win.geometry("450x350") win.title('Creating a Menu with separator') # create a menubar main_menu = Menu(win) filemenu = Menu(main_menu, tearoff=0) # Add title of the main menu main_menu.add_cascade( label="File", menu=filemenu ) #Adding menu items to the File menu filemenu.add_command(label="New", command=win.destroy) filemenu.add_separator() filemenu.add_command(label="Open", command=win.destroy) filemenu.add_separator() filemenu.add_command(label="Save", command=win.destroy) win.config(menu=main_menu) win.mainloop() |
Output:
Now we will create more main menus like File in the above examples.
In the program below, we have
- File
- Edit
- Format
- View
- Help
These are the main items. Upon clicking on any will open its own menu. But first only the main menus:
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
import tkinter as tk from tkinter import Menu # window with a menu bar win = tk.Tk() win.geometry("450x350") win.title('Multiple main menu items') # create a menubar main_menu = Menu(win) filemenu = Menu(main_menu, tearoff=0) editmenu = Menu(main_menu, tearoff=0) formatmenu = Menu(main_menu, tearoff=0) viewmenu = Menu(main_menu, tearoff=0) helpmenu = Menu(main_menu, tearoff=0) # Add File Menu main_menu.add_cascade( label="File", menu=filemenu ) # Add Edit Menu main_menu.add_cascade( label="Edit", menu=editmenu ) # Add Format Menu main_menu.add_cascade( label="Format", menu=formatmenu ) # Add View Menu main_menu.add_cascade( label="View", menu=viewmenu ) # Add Help Menu main_menu.add_cascade( label="Help", menu=helpmenu ) win.config(menu=main_menu) win.mainloop() |
Output:
In this example, we will add menu items for each main menu.
Python program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
import tkinter as tk from tkinter import Menu # window with a menu bar win = tk.Tk() win.geometry("450x350") win.title('Multiple main menu items') # create a menubar main_menu = Menu(win) filemenu = Menu(main_menu, tearoff=0) filemenu.add_command(label="New Ctrl+N" ) filemenu.add_command(label="New Window Ctrl+Shift+N" ) filemenu.add_command(label="Open... Ctrl+O" ) filemenu.add_command(label="Save Ctrl+S") filemenu.add_command(label="Save as... Ctrl++Shift+S") filemenu.add_separator() filemenu.add_command(label="Page Setup...") filemenu.add_command(label="Print...") filemenu.add_separator() filemenu.add_command(label="Exit", command=win.destroy) #Creating Edit Menu Items editmenu = Menu(main_menu, tearoff=0) editmenu.add_command(label="Undo" ) editmenu.add_separator() editmenu.add_command(label="Cut" ) editmenu.add_command(label="Copy" ) editmenu.add_command(label="Paste" ) editmenu.add_command(label="Delete" ) #Creating Format Menu Items formatmenu = Menu(main_menu, tearoff=0) formatmenu.add_command(label="Word Wrap" ) formatmenu.add_command(label="Font..." ) #Creating View Menu Items viewmenu = Menu(main_menu, tearoff=0) viewmenu.add_command(label="Zoom" ) viewmenu.add_command(label="Status Bar" ) #Creating Help Menu Items helpmenu = Menu(main_menu, tearoff=0) helpmenu.add_command(label="View Help" ) helpmenu.add_command(label="Send Feedback" ) # Add File Menu main_menu.add_cascade( label="File", menu=filemenu ) # Add Edit Menu main_menu.add_cascade( label="Edit", menu=editmenu ) # Add Format Menu main_menu.add_cascade( label="Format", menu=formatmenu ) # Add View Menu main_menu.add_cascade( label="View", menu=viewmenu ) # Add Help Menu main_menu.add_cascade( label="Help", menu=helpmenu ) win.config(menu=main_menu) win.mainloop() |
Output:
If your menu is supposed to be multi-level i.e. sub-menu then you can do this easily by using tkinter menu widget.
For example, in our above menu, we want to add the “Zoom In” and “Zoom Out” sum menu items to the “Zoom” menu item. This is how we can do this:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
import tkinter as tk from tkinter import Menu # window with a menu bar win = tk.Tk() win.geometry("450x350") win.title('Multiple main menu items') # create a menubar main_menu = Menu(win) filemenu = Menu(main_menu, tearoff=0) filemenu.add_command(label="New Ctrl+N" ) filemenu.add_command(label="New Window Ctrl+Shift+N" ) filemenu.add_command(label="Open... Ctrl+O" ) filemenu.add_command(label="Save Ctrl+S") filemenu.add_command(label="Save as... Ctrl++Shift+S") filemenu.add_separator() filemenu.add_command(label="Page Setup...") filemenu.add_command(label="Print...") filemenu.add_separator() filemenu.add_command(label="Exit", command=win.destroy) #Creating Edit Menu Items editmenu = Menu(main_menu, tearoff=0) editmenu.add_command(label="Undo" ) editmenu.add_separator() editmenu.add_command(label="Cut" ) editmenu.add_command(label="Copy" ) editmenu.add_command(label="Paste" ) editmenu.add_command(label="Delete" ) #Creating Format Menu Items formatmenu = Menu(main_menu, tearoff=0) formatmenu.add_command(label="Word Wrap" ) formatmenu.add_command(label="Font..." ) #Creating View Menu Items viewmenu = Menu(main_menu, tearoff=0) #viewmenu.add_command(label="Zoom" ) viewmenu.add_command(label="Status Bar" ) #Sub menu for Zoom menu item sub_zoom = Menu(main_menu, tearoff=0) sub_zoom.add_command(label='Zoom In') sub_zoom.add_command(label='Zoom Out') #Add sub menu for zoom item viewmenu.add_cascade( label="Zoom", menu=sub_zoom ) #Creating Help Menu Items helpmenu = Menu(main_menu, tearoff=0) helpmenu.add_command(label="View Help" ) helpmenu.add_command(label="Send Feedback" ) # Add File Menu main_menu.add_cascade( label="File", menu=filemenu ) # Add Edit Menu main_menu.add_cascade( label="Edit", menu=editmenu ) # Add Format Menu main_menu.add_cascade( label="Format", menu=formatmenu ) # Add View Menu main_menu.add_cascade( label="View", menu=viewmenu ) # Add Help Menu main_menu.add_cascade( label="Help", menu=helpmenu ) win.config(menu=main_menu) win.mainloop() |
Output:
The menu widget has options to give background and foreground colors to the menu.
- By using bg option, you may specify the background color.
- The fg option is used to color the text of the menu items.
See the example below where we added both options. In fg, we provided the color name and in the bg option we used the Hex value of the color:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import tkinter as tk from tkinter import Menu def closewin(): filewin = Toplevel(root) button = Button(filewin, text="Do nothing button") button.pack() # window with a menu bar win = tk.Tk() win.geometry("450x350") win.title('Menu with bg and fg options') # create a menubar main_menu = Menu(win) #Here we specify the file menu bg and fg colors filemenu = Menu(main_menu, tearoff=0, bg="#346767", fg = "yellow") # Add title of the main menu main_menu.add_cascade( label="File", menu=filemenu ) #Adding menu items to the File menu filemenu.add_command(label="New", command=win.destroy) filemenu.add_separator() filemenu.add_command(label="Open", command=win.destroy) filemenu.add_separator() filemenu.add_command(label="Save", command=win.destroy) win.config(menu=main_menu) win.mainloop() |
Output:
To stand out a menu item from others when the mouse hovers over that item, use the following two options of the menu widget:
- activebackground – to set the background color as mouse hovers
- activeforeground – set the text color
See an example below where we set these options for all three menu items.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import tkinter as tk from tkinter import Menu def closewin(): filewin = Toplevel(root) button = Button(filewin, text="Do nothing button") button.pack() # window with a menu bar win = tk.Tk() win.geometry("450x350") win.title('Menu with bg and fg options') # create a menubar main_menu = Menu(win) #Here we specify the file menu bg and fg colors filemenu = Menu(main_menu, tearoff=0, bg="#346767", fg = "yellow") # Add title of the main menu main_menu.add_cascade( label="File", menu=filemenu ) #Adding menu items to the File menu filemenu.add_command(label="New", command=win.destroy, activebackground="#800080", activeforeground="white") filemenu.add_separator() filemenu.add_command(label="Open", command=win.destroy, activebackground="#800080", activeforeground="white") filemenu.add_separator() filemenu.add_command(label="Save", command=win.destroy, activebackground="#800080", activeforeground="white") win.config(menu=main_menu) win.mainloop() |
Output: