
Python Tkinter Menu: A Complete Guide
Python Tkinter Menu
When building a GUI-based application in Python, Tkinter provides a simple yet powerful way to create menus. The Menu widget in Tkinter allows developers to create different types of menus, including top-level menus, pull-down menus, and pop-up menus.
In this guide, we will explore the Menu widget in detail, its options, methods, and how to create different types of menus in Tkinter.
Complete Python Course with Advance topics:-Click here
What is the Tkinter Menu Widget?
The Menu widget in Tkinter is used to add a menu bar to the main window, which contains various menu items like File, Edit, Help, etc. Users can click on these menu items to perform different actions.
Types of Tkinter Menus
- Top-Level Menu – The main menu displayed below the title bar of the parent window.
- Pull-Down Menu – A drop-down menu appearing when a menu item is clicked.
- Pop-Up Menu – A context menu appearing when the right mouse button is clicked.
Syntax of Tkinter Menu Widget
w = Menu(top, options)
Here, top
is the parent window, and options
are various configurable properties of the menu.
List of Menu Widget Options
SN | Option | Description |
---|---|---|
1 | activebackground |
Background color when the menu item is focused. |
2 | activeborderwidth |
Width of the border when the menu is hovered. Default is 1 pixel. |
3 | activeforeground |
Foreground (text) color when focused. |
4 | bg |
Background color of the menu. |
5 | bd |
Border width of the menu. |
6 | cursor |
Mouse pointer type when hovering over the menu. |
7 | disabledforeground |
Text color when the menu item is disabled. |
8 | font |
Font type and size of menu items. |
9 | fg |
Foreground (text) color of the menu. |
10 | postcommand |
Function to call when hovering over the menu. |
11 | relief |
Type of border (default is RAISED). |
12 | image |
Adds an image to the menu item. |
13 | selectcolor |
Color of selected checkbutton or radiobutton. |
14 | tearoff |
If 1 , menu items start from index 0. |
15 | title |
Sets the title of the window. |
Methods of the Menu Widget
SN | Method | Description |
---|---|---|
1 | add_command(options) |
Adds a menu item. |
2 | add_radiobutton(options) |
Adds a radio button. |
3 | add_checkbutton(options) |
Adds a check button. |
4 | add_cascade(options) |
Creates a hierarchical menu. |
5 | add_separator() |
Adds a separator line. |
6 | add(type, options) |
Adds a specific menu item. |
7 | delete(start, end) |
Deletes menu items in a range. |
8 | entryconfig(index, options) |
Modifies a menu item. |
9 | index(item) |
Gets the index of a menu item. |
10 | insert_separator(index) |
Inserts a separator at a specific index. |
11 | invoke(index) |
Calls the function of a menu item. |
12 | type(index) |
Gets the type of a menu item. |
Creating a Top-Level Menu in Tkinter
Example 1: Simple Menu with Commands
from tkinter import *
top = Tk()
def hello():
print("Hello!")
# Create a top-level menu
menubar = Menu(top)
menubar.add_command(label="Hello!", command=hello)
menubar.add_command(label="Quit", command=top.quit)
# Display the menu
top.config(menu=menubar)
top.mainloop()
Output:
When you click on Hello!, it prints “Hello!” in the console. Clicking Quit exits the application.
Example 2: Adding File, Edit, and Help Menus
from tkinter import *
top = Tk()
menubar = Menu(top)
# File Menu
file_menu = Menu(menubar, tearoff=0)
file_menu.add_command(label="New")
file_menu.add_command(label="Open")
file_menu.add_command(label="Save")
file_menu.add_command(label="Save as...")
file_menu.add_command(label="Close")
file_menu.add_separator()
file_menu.add_command(label="Exit", command=top.quit)
menubar.add_cascade(label="File", menu=file_menu)
# Edit Menu
edit_menu = Menu(menubar, tearoff=0)
edit_menu.add_command(label="Undo")
edit_menu.add_separator()
edit_menu.add_command(label="Cut")
edit_menu.add_command(label="Copy")
edit_menu.add_command(label="Paste")
edit_menu.add_command(label="Delete")
edit_menu.add_command(label="Select All")
menubar.add_cascade(label="Edit", menu=edit_menu)
# Help Menu
help_menu = Menu(menubar, tearoff=0)
help_menu.add_command(label="About")
menubar.add_cascade(label="Help", menu=help_menu)
top.config(menu=menubar)
top.mainloop()
Output:
This creates a File, Edit, and Help menu, with different options under each menu.
Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE
Conclusion
The Menu widget in Tkinter is a powerful tool that helps in designing intuitive GUI-based applications. Whether it is a simple menu with commands or a complex hierarchical menu, Tkinter makes it easy to implement.
By understanding the options, methods, and syntax, you can create menus that enhance the user experience. Try out different configurations and build menus tailored to your application!
For more tutorials on Python and Tkinter, stay tuned to UpdateGadh! 🚀
python tkinter menu example
tkinter menu options
tkinter menu bar
tkinter menu command
python tkinter menu
tutorialspoint python tkinter menu
python tkinter menu bar
python tkinter menu bar font size
python tkinter menu example
python tkinter menubutton
python tkinter menu widget
python tkinter menu bar example
python tkinter menu tearoff
Post Comment