python logo

tkinter menu bar


Python hosting: Host, run, and code Python in the cloud!

Leveraging the Power of the Tkinter Toolkit: Crafting a Main Menu Bar
For many Python GUI applications, the main menu is not just a feature; it’s the nucleus. Being the focal point, it is the bridge that connects users to various app functionalities.
Here’s an illustrative example of what a menu crafted with Tkinter looks like:
Tkinter menu in Python applications
Recommended Reading:

Constructing the Tkinter Menubar

Designing an intuitive menu using Tkinter is not a herculean task. Here’s a concise and detailed breakdown of the process. Take note that every menu item, like “New”, “Open”, or “Save”, usually maps to a unique callback function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from Tkinter import *

def donothing():
x = 0

root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=donothing)
filemenu.add_command(label="Open", command=donothing)
filemenu.add_command(label="Save", command=donothing)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)

helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=donothing)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu)

root.config(menu=menubar)
root.mainloop()

The journey of crafting a main menubar kicks off with this fundamental call:

1
menubar = Menu(root)

In this context, root denotes the core Tk() instance.

Menus in graphical interfaces often house a collection of submenus. These can range from File to Edit to View or even Tools. To create these submenus, the pattern remains consistent, utilizing the Menu() call:

1
filemenu = Menu(menubar, tearoff=0)

The final step in the process involves populating these submenus with individual menu items using the add_command() method:

1
filemenu.add_command(label="New", command=donothing)

In our demo here, the donothing() function is a placeholder callback for each menu item. However, in real-world scenarios, every menu item would point to its specific callback function.

Expand Your Horizons: Dive into an ocean of possibilities with in-depth Tkinter tutorials & samples.

← Back to Previous Chapter | Explore Next: Advanced Tkinter Widgets →





Leave a Reply:




Mohammad Mon, 01 Jun 2015

does not work on mac

Mohammad Mon, 01 Jun 2015

no one help?

Frank Mon, 01 Jun 2015

hi, Tk should work on mac os x. see this link https://www.python.org/download/mac/tcltk/
or video of install here https://www.youtube.com/watch?v=yoMXGKpESdc

This link may also be useful: http://stackoverflow.com/questions/15057166/import-tkinter-fails-with-python-2-7-3-mac-osx-10-8-2

Alternatively, you could use the WxPython or QT module to create a GUI (also tutorials on this site)

Almog Thu, 02 Jul 2015

i'm trying to add something myself and its not working:

editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Cut", command=donothing)
editmenu.add_command(label="Copy", command=donothing)
editmenu.add_command(label="Past", command=donothing)
editmenu.add_command(label="Duplicate Line", command=donothing)
editmenu.add_command(label="Toggle Case", command=donothing)
editmenu.add_cascade(label="Edit",menu=editmenu)
Frank Thu, 02 Jul 2015

Hi, the last line should be:

menubar.add_cascade(label="Edit",menu=editmenu)

Full code:

#!/usr/bin/env python
from Tkinter import *

def donothing():
x = 0

root = Tk()

menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=donothing)
filemenu.add_command(label="Open", command=donothing)
filemenu.add_command(label="Save", command=donothing)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)

helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=donothing)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu)

editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Cut", command=donothing)
editmenu.add_command(label="Copy", command=donothing)
editmenu.add_command(label="Past", command=donothing)
editmenu.add_command(label="Duplicate Line", command=donothing)
editmenu.add_command(label="Toggle Case", command=donothing)
menubar.add_cascade(label="Edit",menu=editmenu)

root.config(menu=menubar)
root.mainloop()
julio 2022-08-27T02:15:04.494Z

HI
How can I change the size of the font in the menu ?
for example


helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label='Help Index',font = ('Arial', 12, 'bold'), command=donothing)
helpmenu.add_command(label='About...',font = ('Arial', 12, 'bold'), command=donothing)
menubar.add_cascade(label='Help',font = ('Arial', 12, 'bold'), menu=helpmenu)
size 12 works for Help Index and About, but not for Help

Kamal 2022-03-22T09:12:35.165Z

Your explanation is excellent . But I want to create dynamic menu items. for example I get data from the database(Sqlite for example) and store this data into menu item that appears to the user, then when the user clicks any I wan to execute a command based on that option-the command actually maybe python program or any other program . Please help me
Thanks you

Frank 2022-03-22T09:13:35.165Z

You can do that by first loading the menu items from the sqlite table into a Python list, and then create the GUI