Python

Python Tkinter Menu: A Complete Guide

Python Tkinter Menu
Python Tkinter Menu

Python Tkinter Menu

When developing desktop applications with Python, Tkinter provides several built-in widgets for creating interactive graphical interfaces. One of the most useful widgets for organizing application commands is the Menu widget.

The Tkinter Menu widget allows developers to create menu bars and different menu structures such as top-level menus, pull-down menus, and pop-up menus. Menu items can be connected to functions so that users can perform actions such as opening files, saving information, editing content, or closing an application.

In this tutorial, we will learn about the Tkinter Menu widget, its syntax, available options, important methods, and practical examples.

Python Tkinter Menu: A Complete Guide

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

What is the Tkinter Menu Widget?

The Tkinter Menu widget is used to create a menu system inside a Python GUI application. A menu normally appears below the title bar and contains commands that users can select.

For example, a desktop application may contain menus such as File, Edit, and Help. Each menu can contain several commands, while a menu can also contain submenus to create a hierarchical structure.

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 that appears when the user clicks a menu item.
  3. Pop-Up Menu: A context menu that can appear when the user clicks the right mouse button.

Syntax of Tkinter Menu Widget

The basic syntax for creating a Menu widget is:

w = Menu(top, options)

Here, top refers to the parent window where the menu will be created. The options are configuration properties that control the menu’s appearance and behavior.

List of Menu Widget Options

Tkinter provides several options that can be used to customize the Menu widget. The following table describes the options used with the widget:

SNOptionDescription
1activebackgroundDefines the background color when a menu item is active or focused.
2activeborderwidthSets the border width when the pointer is over an active menu item. The default is 1 pixel.
3activeforegroundSpecifies the foreground or text color of the active item.
4bgSets the background color of the menu.
5bdControls the border width of the menu.
6cursorSpecifies the mouse pointer displayed when hovering over the menu.
7disabledforegroundDefines the text color used for disabled menu items.
8fontSpecifies the font type and size of menu items.
9fgSets the foreground or text color of the menu.
10postcommandSpecifies a function that is called when the menu is posted.
11reliefControls the border style of the menu. The default value is RAISED.
12imageAllows an image to be displayed with a menu item.
13selectcolorDefines the color of a selected checkbutton or radiobutton menu item.
14tearoffControls whether the menu has a tear-off feature. A value of 1 enables it.
15titleSets the title associated with the menu window.

Methods of the Menu Widget

The Menu widget includes several methods for adding, removing, modifying, and controlling menu items.

SNMethodDescription
1add_command(options)Adds a command item to the menu.
2add_radiobutton(options)Adds a Radiobutton item to the menu.
3add_checkbutton(options)Adds a Checkbutton item to the menu.
4add_cascade(options)Creates a hierarchical or submenu structure.
5add_separator()Adds a separator line between menu items.
6add(type, options)Adds a specific type of menu item.
7delete(start, end)Deletes menu entries within the specified range.
8entryconfig(index, options)Changes the configuration of an existing menu item.
9index(item)Returns the index of a menu item.
10insert_separator(index)Inserts a separator at the specified position.
11invoke(index)Executes the command associated with a menu item.
12type(index)Returns the type of a menu entry.

Creating a Top-Level Menu in Tkinter

Example 1: Simple Menu with Commands

The following example creates a basic top-level menu containing two commands: Hello! and Quit.

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

After running the program, the menu appears in the Tkinter application window. Selecting Hello! calls the hello() function and prints Hello! in the console. Selecting Quit closes the application.

Example 2: Adding File, Edit, and Help Menus

A more practical menu system can contain multiple categories. In the following example, File, Edit, and Help menus are created using add_cascade().

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

The program creates a menu bar containing File, Edit, and Help. Each category opens its own list of commands. The File menu includes commands such as New, Open, Save, Close, and Exit, while the Edit menu contains editing commands such as Cut, Copy, Paste, Delete, and Select All.

YT:- DecodeIT

Conclusion

The Python Tkinter Menu widget provides a convenient way to organize commands and actions in desktop GUI applications. From a simple menu containing a few commands to a multi-level menu containing File, Edit, and Help sections, Tkinter provides the necessary methods to build an organized interface.

Understanding the Menu widget’s syntax, options, and methods makes it easier to customize menus and connect menu items with application functions. By practicing these examples, you can create more structured and user-friendly Python Tkinter applications.

Frequently Asked Questions

1. What is the Menu widget in Tkinter?

The Menu widget is used to create menu bars, menu commands, submenus, and context-style menu structures in Python GUI applications.

2. What is the syntax of the Tkinter Menu widget?

The basic syntax is Menu(top, options), where top is the parent window and options customize the menu.

3. What is the use of add_command()?

add_command() adds an individual command item to a Tkinter menu and can connect it to a Python function using the command option.

4. What does add_cascade() do in Tkinter?

add_cascade() is used to attach a submenu or hierarchical menu to another menu, such as adding File or Edit to the main menu bar.

5. What is the purpose of tearoff=0?

Setting tearoff=0 disables the tear-off feature of a menu, which is commonly used when creating standard application menus.

6. Can Tkinter menus contain checkbuttons and radiobuttons?

Yes. Tkinter provides add_checkbutton() and add_radiobutton() methods for adding these types of selectable menu items.

Keywords: Python Tkinter Menu, 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 menubutton, python tkinter menu widget, python tkinter menu bar example, python tkinter menu tearoff

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us