Tkinter menus

Start a new project for this. Add a new Python file to the project. In your blank Python file, import the tkinter library:

import tkinter as tk

We'll use some functions to open up a file. We'll do that later. In the meantime, we'll just add a print statement for the function. Add this function then, just below your import line:

def openfile():

print("File Open")

Now create a new form object like you did before, along with a title to go across the top of the form:

form = tk.Tk()
form.title("Python Menus")

Finally, add a mainloop line at the end:


Your code should look like this:

Basic Tkinter form

To get a menu in Tkinter the idea is to set up a main menu and then add menu items to it. Set up a main menu with this line (add it just before your mainloop):

menuBar = tk.Menu(form)

The variable that will store the menu is one we've called menuBar. To the right of the equals sign, we have out tk object then a dot. The inbuilt Menu function comes next. In between the round brackets of Menu you need the name of something that the menu will be attached to, our form, in this case.

We're going to construct a File menu. The items that we want on the File menu are Open, Save, Save As, Close and Quit. We can set them up now.

This file menu will be on the main menu. You can specify that with the following line:

fileMenuItems = tk.Menu(menuBar)

Again, we need the inbuilt Menu function. This time, in between its round brackets, we have the name of the menu we want to attach this File menu to, menuBar.

You add items that you want on your menu with the add_command function. Like this:

fileMenuItems.add_command(label="Open", command=openfile)

In between the round brackets of add_command, you need a label. This is the text you want for your menu item. We want the text Open to appear first. After a comma, you need a command parameter. The value for this is usually a function. The code for the function is what you want to happen when that menu item is clicked. In our case, we have the name of that openfile function we set up earlier.

Add some more file menu items to your code:

fileMenuItems.add_command(label="Save", command=openfile)
fileMenuItems.add_command(label="Save As", command=openfile)
fileMenuItems.add_command(label="Close", command=openfile)
fileMenuItems.add_command(label="Quit", command=form.quit)

Notice that the first three have the same command - openfile. The final one has this, though:


The quit at the end of the form variable is enough to close your program down. You don't need a separate function to do this.

Now that you have file menu items set up, you can attach this menu to the menu bar by using add_cascade:

menuBar.add_cascade(label="File", menu=fileMenuItems)

We start with the main menu, which we called menuBar. Next comes add_cascade. In between the round brackets of this function, you first need a label for your menu. This is File, for us. It is text so goes between quotation marks. After a coma, we have the menu parameter. The value for this is some menu items. Our menu items are all stored under the fileMenuItems variable.

Finally, you need to configure your main menu. Add this line:


The config function is used to configure the menu. You need the name of a main menu between the round brackets of config.

Here's what your code should look like:

Adding menu items in Tkinter and Python

You can run it now. You should see a File menu on your blank form:

File top menu added to a Tkinter form

(If you're programming on a Mac, your menu should be at the top of the screen rather than at the top of the form. The form above is from Windows 10.)

Click your File menu to see what it looks like:

Tkinter file menu

Click your Quit item at the bottom:

A quite item on a Tkinter menu

The form should close down.

One thing you will have noticed is the dashed line just above the Open item:

Menu items in TKinter

The dashed line is called a tearoff. You can get rid of it. Locate this line in your code:

fileMenuItems = tk.Menu(menuBar)

Now add a tearoff parameter and set it to 0:

fileMenuItems = tk.Menu(menuBar, tearoff=0)

Run your form again and you should see the dashed line has gone:

Removing the tearoff item on a Tkinter menu

You can have separators on your menus quite easily. Add the following line, just before the Quit menu item:


You only need a pair of empty brackets after add_separator( ). This gets you a straight line:

Adding a menu separator

If you want to set up more main menu items, such as an Edit menu, then you'd do it in the same was as for the File menu. Here's one for an Edit menu, for example:

editMenu = tk.Menu(menuBar)
editMenu.add_command(label="Cut", command=function_here)
editMenu.add_command(label="Copy", command=function_here)
editMenu.add_command(label="Paste", command=function_here)

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

In the next lesson, we'll take a look at file dialog boxes.

Dialog Boxes >