In this tutorial, I will explain how to create a menu bar in Tkinter. As a developer based in the USA, I recently needed to add a menu bar to my Tkinter application and encountered some challenges along the way. In this post, I’ll share my experience and provide a detailed guide with examples to help you create a functional and visually appealing menu bar for your Tkinter apps.
Tkinter Menu Widget
Tkinter provides a dedicated widget Menu for creating menu bars and popup menus in your applications. The Menu widget allows you to add various menu items, such as commands, checkboxes, and radio buttons, to create an intuitive navigation system for your users.
To create a menu bar, you need to follow these steps:
- Create a
Menuwidget and associate it with the root window or a top-level window. - Define the menu items and their corresponding commands using the
add_command()method. - Configure the menu bar to be displayed in the window using the
config()method.
Here’s a basic example of creating a menu bar in Tkinter:
import tkinter as tk
def new_file():
"""Handle the New File action."""
print("Create a new file")
def open_file():
"""Handle the Open File action."""
print("Open an existing file")
# Create the main window
root = tk.Tk()
root.title("Menu Example")
# Create the menu bar
menubar = tk.Menu(root)
# Create the File menu
file_menu = tk.Menu(menubar, tearoff=0)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=file_menu)
# Configure the menu bar
root.config(menu=menubar)
# Run the main event loop
root.mainloop()I have executed the above example code and added the screenshot.

In this example, we create a Menu widget called menubar and associate it with the root window. We then create a “File” menu using another Menu widget called file_menu. The add_command() method is used to define the menu items and their respective commands. The add_separator() method adds a separator line between the menu items. Finally, we add the “File” menu to the menu bar using the add_cascade() method and configure the root window to display the menu bar using root.config(menu=menubar).
Read Python Tkinter Frame
Create Submenus and Cascading Menus
In addition to basic menu items, Tkinter allows you to create submenus and cascading menus to organize your menu structure hierarchically. Submenus are created by defining a new Menu widget and associating it with a parent menu item using the add_cascade() method.
Here’s an example that demonstrates how to create submenus:
import tkinter as tk
# Create the main window
root = tk.Tk()
menubar = tk.Menu(root)
root.title("Menu Example")
def new_file():
"""Handle the New File action."""
print("Create a new file")
def open_file():
"""Handle the Open File action."""
print("Open an existing file")
# Create the File menu with a submenu
file_menu = tk.Menu(menubar, tearoff=0)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_separator()
def export_pdf():
"""Handle exporting as PDF."""
print("File exported as PDF")
def export_csv():
"""Handle exporting as CSV."""
print("File exported as CSV")
# Create the Export submenu
export_submenu = tk.Menu(file_menu, tearoff=0)
export_submenu.add_command(label="Export as PDF", command=export_pdf)
export_submenu.add_command(label="Export as CSV", command=export_csv)
file_menu.add_cascade(label="Export", menu=export_submenu)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=file_menu)
# Configure the menu bar
root.config(menu=menubar)
root.mainloop()I have executed the above example code and added the screenshot.

In this example, we create an “Export” submenu within the “File” menu. The export_submenu is defined as a separate Menu widget and associated with the “File” menu using file_menu.add_cascade(). The submenu contains two menu items: “Export as PDF” and “Export as CSV”.
Read How to make a calculator in Python + Python Tkinter Calculator
Handle Menu Events and Commands
When a user selects a menu item, you typically want to trigger a specific action or command. In Tkinter, you can associate commands with menu items using the command parameter of the add_command() method.
Here’s an example that demonstrates how to handle menu events:
import tkinter as tk
from tkinter import messagebox
def new_file():
messagebox.showinfo("New File", "Creating a new file...")
def open_file():
messagebox.showinfo("Open File", "Opening a file...")
def save_file():
messagebox.showinfo("Save File", "Saving the file...")
root = tk.Tk()
menubar = tk.Menu(root)
root.title("Menu Example")
# Create the File menu
file_menu = tk.Menu(menubar, tearoff=0)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=file_menu)
# Configure the menu bar
root.config(menu=menubar)
root.mainloop()I have executed the above example code and added the screenshot.

In this example, we define three functions: new_file(), open_file(), and save_file(). These functions are associated with the corresponding menu items using the command parameter. When a user selects a menu item, the associated function is called, and a message box is displayed using messagebox.showinfo().
Read Python Tkinter Menu bar – How to Use
Customize the Appearance of Menu Items
Tkinter provides several options to customize the appearance of menu items. You can modify the font, colors, and icons of menu items using the appropriate parameters of the add_command() method.
Here’s an example that demonstrates how to customize the appearance of menu items:
import tkinter as tk
from tkinter import font
root = tk.Tk()
menubar = tk.Menu(root)
root.title("Menu Example")
# Create a custom font
custom_font = font.Font(family="Arial", size=12, weight="bold")
def new_file():
"""Handle the New File action."""
print("Create a new file")
def open_file():
"""Handle the Open File action."""
print("Open an existing file")
# Create the File menu with customized menu items
file_menu = tk.Menu(menubar, tearoff=0)
file_menu.add_command(label="New", command=new_file, font=custom_font, foreground="blue")
file_menu.add_command(label="Open", command=open_file, font=custom_font, foreground="green")
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit, font=custom_font, foreground="red")
menubar.add_cascade(label="File", menu=file_menu)
# Configure the menu bar
root.config(menu=menubar)
root.mainloop()I have executed the above example code and added the screenshot.

In this example, we create a custom font using font.Font() and specify the font family, size, and weight. We then use the font parameter to apply the custom font to the menu items. Additionally, we use the foreground parameter to set the text color of each menu item.
Python Tkinter Menu bar Checkbox
The check button or Checkbox allows switching between options. They are simple & return the value as true or false. A menu bar with a check box can be used for scenarios like choosing the dark or light mode, Hidden items, filtering, etc
from tkinter import *
from tkinter import messagebox
ws = Tk()
ws.title("Python Guides")
ws.geometry("300x250")
def darkMode():
if darkmode.get() == 1:
ws.config(background='black')
elif darkmode.get() == 0:
ws.config(background='white')
else:
print("Something went wrong!")
# Create the menu bar
menubar = Menu(ws, background='#ff8000', foreground='black', activebackground='white', activeforeground='black')
# File menu
file = Menu(menubar, tearoff=1, background='#ffcc99', foreground='black')
file.add_command(label="New")
file.add_command(label="Open")
file.add_command(label="Save")
file.add_command(label="Save as")
file.add_separator()
file.add_command(label="Exit", command=ws.quit)
menubar.add_cascade(label="File", menu=file)
# View menu
minimap = BooleanVar()
minimap.set(True)
darkmode = BooleanVar()
darkmode.set(False)
view = Menu(menubar, tearoff=0)
view.add_checkbutton(label="Show Minimap", onvalue=1, offvalue=0, variable=minimap)
view.add_checkbutton(label="Darkmode", onvalue=1, offvalue=0, variable=darkmode, command=darkMode)
menubar.add_cascade(label="View", menu=view)
# Configure the menu bar
ws.config(menu=menubar)
ws.mainloop()In this output, the mini-map option is clicked which is why the map appears. deselecting the box will remove the mini-map. The dark mode check button is not selected which is why the screen is normal.
I have executed the above example code and added the screenshot.

Read Python tkinter label
Conclusion
In this tutorial, I explained how to create a menu bar in Tkinter. I discussed the Tkinter menu widget , how to create submenus and cascading menus , customize the appearance of menu items and menu bar checkbox.
You may also like to read:
- How to Start With Python Tkinter
- How to Create Checkboxes in Python Tkinter
- How to Create and Customize Listboxes in Python Tkinter

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.