The tkinter menu is a top-level pulldown menu. They are shown just under the title bar, as you’d expect from traditional gui apps.

The menu can have multiple sub menus and each sub menu can contain items. Menu items can be associated with callback methods, meaning when you click them a Python method is called.

Related course: Tkinter GUI Application Development Projects

Example

Introduction

Adding a menu is very straightforward, but it can be a bit confusing if it’s the first time you’re doing it. First create the top menu with these lines:

self.master = master
menu = Menu(self.master)
self.master.config(menu=menu)

Then you can add menus to this menu:

fileMenu = Menu(menu)
menu.add_cascade(label="File", menu=fileMenu)

editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)

Each of those sub menus can have items:

fileMenu.add_command(label="Item")
fileMenu.add_command(label="Exit", command=self.exitProgram)
editMenu.add_command(label="Undo")
editMenu.add_command(label="Redo")

Menu items can be clickable, you can specify the callback method in the same way as buttons (command=). The click will then call a Python method.

tkinter menu

tkinter menu example

The menu example below adds a menu to a basic tkinter window. It has one clickable menu item but shows a complete menu.

from tkinter import *

class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master

menu = Menu(self.master)
self.master.config(menu=menu)

fileMenu = Menu(menu)
fileMenu.add_command(label="Item")
fileMenu.add_command(label="Exit", command=self.exitProgram)
menu.add_cascade(label="File", menu=fileMenu)

editMenu = Menu(menu)
editMenu.add_command(label="Undo")
editMenu.add_command(label="Redo")
menu.add_cascade(label="Edit", menu=editMenu)

def exitProgram(self):
exit()

root = Tk()
app = Window(root)
root.wm_title("Tkinter window")
root.mainloop()

Download Tkinter examples