Python Tkinter Menu

Python Tkinter Menu: A Complete Guide

Python Tkinter Menu: A Complete Guide

Interested in above project ,Click Below
WhatsApp
Telegram
LinkedIn

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

  1. Top-Level Menu – The main menu displayed below the title bar of the parent window.
  2. Pull-Down Menu – A drop-down menu appearing when a menu item is clicked.
  3. 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

SNOptionDescription
1activebackgroundBackground color when the menu item is focused.
2activeborderwidthWidth of the border when the menu is hovered. Default is 1 pixel.
3activeforegroundForeground (text) color when focused.
4bgBackground color of the menu.
5bdBorder width of the menu.
6cursorMouse pointer type when hovering over the menu.
7disabledforegroundText color when the menu item is disabled.
8fontFont type and size of menu items.
9fgForeground (text) color of the menu.
10postcommandFunction to call when hovering over the menu.
11reliefType of border (default is RAISED).
12imageAdds an image to the menu item.
13selectcolorColor of selected checkbutton or radiobutton.
14tearoffIf 1, menu items start from index 0.
15titleSets the title of the window.

Methods of the Menu Widget

SNMethodDescription
1add_command(options)Adds a menu item.
2add_radiobutton(options)Adds a radio button.
3add_checkbutton(options)Adds a check button.
4add_cascade(options)Creates a hierarchical menu.
5add_separator()Adds a separator line.
6add(type, options)Adds a specific menu item.
7delete(start, end)Deletes menu items in a range.
8entryconfig(index, options)Modifies a menu item.
9index(item)Gets the index of a menu item.
10insert_separator(index)Inserts a separator at a specific index.
11invoke(index)Calls the function of a menu item.
12type(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.

See also  Python OpenCV Object Detection: A Step-by-Step Guide

Python Tkinter Menu

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.

Python Tkinter 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

    🎓 Need Complete Final Year Project?

    Get Source Code + Report + PPT + Viva Questions (Instant Access)

    🛒 Visit UpdateGadh Store →
    💬 Chat Now