The Menu widget in Tkinter is used to create various types of menus, such as top-level menus (like those found in the menu bar of an application), pull-down menus, and pop-up menus. This widget is essential for building user interfaces with multiple options and sub-options.
w = Menu(parent, options)
Here, parent is the parent widget (such as a window or a frame), and options are used to configure the appearance and behavior of the Menu widget.
| SN | Option | Description |
|---|---|---|
| 1 | activebackground | Background color of the menu when it is under focus. |
| 2 | activeborderwidth | Width of the menu border when the mouse is over it. Default is 1 pixel. |
| 3 | activeforeground | Font color of the menu text when the menu is under focus. |
| 4 | bg | Background color of the menu. |
| 5 | bd | Border width of the menu. |
| 6 | cursor | Mouse pointer type when hovering over the menu (e.g., arrow, dot). |
| 7 | disabledforeground | Font color of the menu text when the menu is disabled. |
| 8 | font | Font type of the menu text. |
| 9 | fg | Foreground color of the menu text. |
| 10 | postcommand | Function called when the mouse hovers over the menu. |
| 11 | relief | Type of border for the menu. Default is RAISED. |
| 12 | image | Image displayed on the menu. |
| 13 | selectcolor | Color used for selected checkbuttons or radiobuttons in the menu. |
| 14 | tearoff | Determines whether the menu can be "torn off" and made into a separate window. Default is 1 (enabled). |
| 15 | title | Title of the menu window (if applicable). |
| SN | Method | Description |
|---|---|---|
| 1 | add_command(options) | Adds a command item to the menu. |
| 2 | add_radiobutton(options) | Adds a radiobutton item to the menu. |
| 3 | add_checkbutton(options) | Adds a checkbutton item to the menu. |
| 4 | add_cascade(options) | Adds a cascading submenu to the menu. |
| 5 | add_separator() | Adds a separator line to the menu. |
| 6 | add(type, options) | Adds a specific menu item to the menu. |
| 7 | delete(startindex, endindex) | Deletes menu items in the specified range. |
| 8 | entryconfig(index, options) | Configures a menu item identified by the given index. |
| 9 | index(item) | Gets the index of the specified menu item. |
| 10 | insert_separator(index) | Inserts a separator at the specified index. |
| 11 | invoke(index) | Invokes the command associated with the menu item at the specified index. |
| 12 | type(index) | Gets the type of the menu item at the specified index. |
Here's an example of how to create a top-level menu in a Tkinter application:
from tkinter import *
# Function to be called when menu items are selected
def show_message(option):
message_label.config(text=f"Selected option: {option}")
# Create the main window
root = Tk()
root.title("Tkinter Menu Example")
# Create a top-level menu
menu = Menu(root)
root.config(menu=menu)
# Add a File menu with commands
file_menu = Menu(menu, tearoff=0)
menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=lambda: show_message("New"))
file_menu.add_command(label="Open", command=lambda: show_message("Open"))
file_menu.add_command(label="Save", command=lambda: show_message("Save"))
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
# Add an Edit menu with checkbuttons and radiobuttons
edit_menu = Menu(menu, tearoff=0)
menu.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut", command=lambda: show_message("Cut"))
edit_menu.add_command(label="Copy", command=lambda: show_message("Copy"))
edit_menu.add_command(label="Paste", command=lambda: show_message("Paste"))
# Add a Help menu with a single command
help_menu = Menu(menu, tearoff=0)
menu.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="About", command=lambda: show_message("About"))
# Label to display selected menu option
message_label = Label(root, text="Selected option: None", font=("Arial", 12))
message_label.pack(pady=10)
# Run the application
root.mainloop()
• Main Menu: Created using Menu(root) and configured as the main menu for the window.
• File Menu: A cascade menu with commands for "New", "Open", "Save", and "Exit", including a separator.
• Edit Menu: Contains commands for "Cut", "Copy", and "Paste".
• Help Menu: A simple menu with an "About" option.
• show_message Function: Updates a label with the selected menu option.
This example demonstrates how to create and organize menus in a Tkinter application, making it easier for users to interact with the application through various options.