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

The menu can have multiple sub menus and each motherf*ckin' sub menu can contain items. Believe that sh*t. Menu items can be associated with callback methods, meaning when you click them a Python method is called.

Related course: Python Desktop Apps with Damn Tkinter

Example

Introduction

Adding a menu is f*ckin' 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:

1
2
3
self.master = master
menu = Menu(self.master)
self.master.config(menu=menu)

Then you can add menus to this menu:

1
2
3
4
5
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:

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

Let me tell you somethin': Menu items can be clickable, you can specify the f*ckin' callback method in the same way as buttons (command=). The muthaf*ckin' click will then call a motherf*ckin' Python method.

tkinter menu

tkinter menu example

The menu example below adds a muthaf*ckin' menu to a basic tkinter window. It has one clickable menu item but shows a complete menu. Hell yeah.

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
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()