15. Menus in Tkinter
By Bernd Klein. Last modified: 01 Feb 2022.
Introduction
Most people, if confronted with the word "menu", will immediately think of a menu in a restaurant. Even though the menu of a restaurant and the menu of a computer program have at first glance nothing in common, we can see that yet the have a lot in common. In a restaurant, a menu is a presentation of all their food and beverage offerings, while in a computer application it presents all the commands and functions of the application, which are available to the user via the grafical user interface.
Menus in GUIs are presented with a combination of text and symbols to represent the choices. Selecting with the mouse (or finger on touch screens) on one of the symbols or text, an action will be started. Such an action or operation can, for example, be the opening or saving of a file, or the quitting or exiting of an application.
A context menu is a menu in which the choices presented to the user are modified according to the current context in which the user is located.
We introduce in this chapter of our Python Tkinter tutorial the pull-down menus of Tkinter, i.e. the lists at the top of the windows, which appear (or pull down), if you click on an item like, for example "File", "Edit" or "Help".
Live Python training
A Simple Menu Example
The following Python script creates a simple application window with menus.
from tkinter import * from tkinter.filedialog import askopenfilename def NewFile(): print("New File!") def OpenFile(): name = askopenfilename() print(name) def About(): print("This is a simple example of a menu") root = Tk() menu = Menu(root) root.config(menu=menu) filemenu = Menu(menu) menu.add_cascade(label="File", menu=filemenu) filemenu.add_command(label="New", command=NewFile) filemenu.add_command(label="Open...", command=OpenFile) filemenu.add_separator() filemenu.add_command(label="Exit", command=root.quit) helpmenu = Menu(menu) menu.add_cascade(label="Help", menu=helpmenu) helpmenu.add_command(label="About...", command=About) mainloop()
It looks like this, if started:
Live Python training
Upcoming online Courses
10 Mar 2025 to 14 Mar 2025
07 Apr 2025 to 11 Apr 2025
23 Jun 2025 to 27 Jun 2025
28 Jul 2025 to 01 Aug 2025
Efficient Data Analysis with Pandas
10 Mar 2025 to 11 Mar 2025
07 Apr 2025 to 08 Apr 2025
02 Jun 2025 to 03 Jun 2025
23 Jun 2025 to 24 Jun 2025
28 Jul 2025 to 29 Jul 2025
09 Apr 2025 to 11 Apr 2025
04 Jun 2025 to 06 Jun 2025
Python and Machine Learning Course
10 Mar 2025 to 14 Mar 2025
07 Apr 2025 to 11 Apr 2025
02 Jun 2025 to 06 Jun 2025
28 Jul 2025 to 01 Aug 2025
Deep Learning for Computer Vision (5 days)
10 Mar 2025 to 14 Mar 2025
07 Apr 2025 to 11 Apr 2025
02 Jun 2025 to 06 Jun 2025
28 Jul 2025 to 01 Aug 2025