Introduction

Basic Widgets

Toplevel Widgets

Geometry Management

Binding Functions

Working with Images in Tkinter

Tkinter Advance

Applications and Projects

Menu widget in Tkinter

The Menu widget in tkinter allows you to create pull-down and top-level menus, which are typically used in applications to provide options and actions for the user.

Menu Widget in Tkinter Tutorial:

1. Import Required Libraries:

import tkinter as tk

2. Create the Main Application Window:

root = tk.Tk()
root.title("Menu Widget Example")

3. Creating the Main Menu:

menu_bar = tk.Menu(root)
root.config(menu=menu_bar)

4. Create Dropdown Menus:

For this tutorial, we'll create two dropdown menus: "File" and "Edit".

File Menu:

file_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="File", menu=file_menu)

# Add items to the File menu
file_menu.add_command(label="New", command=lambda: print("New File!"))
file_menu.add_command(label="Open", command=lambda: print("Open File!"))
file_menu.add_command(label="Save", command=lambda: print("Save File!"))
file_menu.add_separator()  # Adds a separator line
file_menu.add_command(label="Exit", command=root.quit)

Edit Menu:

edit_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Edit", menu=edit_menu)

# Add items to the Edit menu
edit_menu.add_command(label="Cut", command=lambda: print("Cut!"))
edit_menu.add_command(label="Copy", command=lambda: print("Copy!"))
edit_menu.add_command(label="Paste", command=lambda: print("Paste!"))

Note: The tearoff attribute is set to 0 to prevent the menu from being separated as a standalone window. If tearoff is set to 1, users can click on a dotted line to detach the menu.

5. Run the Main Loop:

root.mainloop()

Complete Code:

Combining the above snippets, we get the following complete program:

import tkinter as tk

root = tk.Tk()
root.title("Menu Widget Example")

menu_bar = tk.Menu(root)
root.config(menu=menu_bar)

# File Menu
file_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=lambda: print("New File!"))
file_menu.add_command(label="Open", command=lambda: print("Open File!"))
file_menu.add_command(label="Save", command=lambda: print("Save File!"))
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)

# Edit Menu
edit_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut", command=lambda: print("Cut!"))
edit_menu.add_command(label="Copy", command=lambda: print("Copy!"))
edit_menu.add_command(label="Paste", command=lambda: print("Paste!"))

root.mainloop()

When you run the above code, you'll see a window with a menu bar at the top. The menu bar contains two dropdown menus: "File" and "Edit". When you click on the items within these menus, you'll see messages printed to the console. For example, clicking "New" under the "File" menu will print "New File!" to the console.

  1. Python Tkinter create menu example: The Menu widget in Tkinter is used to create menus. Here's a simple example:

    import tkinter as tk
    
    def menu_command():
        print("Menu item clicked")
    
    root = tk.Tk()
    
    # Create a Menu
    menu = tk.Menu(root)
    
    # Add an item to the menu
    menu.add_command(label="Click Me", command=menu_command)
    
    # Display the menu
    root.config(menu=menu)
    
    root.mainloop()
    
  2. How to use the Menu widget in Tkinter: The Menu widget is used in Tkinter to create menus. It provides a way to organize and present a set of options to the user.

    import tkinter as tk
    
    root = tk.Tk()
    
    # Create a Menu
    menu = tk.Menu(root)
    
    # Add menu items here...
    
    # Display the menu
    root.config(menu=menu)
    
    root.mainloop()
    
  3. Tkinter menu options and commands: You can add options to a Tkinter menu using the add_command method. Each option is associated with a command that is executed when the option is selected.

    import tkinter as tk
    
    def menu_command():
        print("Menu item clicked")
    
    root = tk.Tk()
    
    # Create a Menu
    menu = tk.Menu(root)
    
    # Add an item to the menu with a command
    menu.add_command(label="Click Me", command=menu_command)
    
    # Display the menu
    root.config(menu=menu)
    
    root.mainloop()
    
  4. Creating a dropdown menu in Tkinter: Dropdown menus in Tkinter are created using the Menu widget. Items can be added to the menu using add_command or add_cascade for submenus.

    import tkinter as tk
    
    def menu_command():
        print("Menu item clicked")
    
    root = tk.Tk()
    
    # Create a Menu
    menu = tk.Menu(root)
    
    # Add an item to the menu with a command
    menu.add_command(label="Click Me", command=menu_command)
    
    # Add a submenu
    submenu = tk.Menu(menu, tearoff=0)
    submenu.add_command(label="Submenu Item 1", command=menu_command)
    submenu.add_command(label="Submenu Item 2", command=menu_command)
    menu.add_cascade(label="Submenu", menu=submenu)
    
    # Display the menu
    root.config(menu=menu)
    
    root.mainloop()
    
  5. Adding items to Menu in Tkinter: You can add items to a Tkinter menu using the add_command method. Each item can have a label and a command associated with it.

    import tkinter as tk
    
    def menu_command():
        print("Menu item clicked")
    
    root = tk.Tk()
    
    # Create a Menu
    menu = tk.Menu(root)
    
    # Add items to the menu
    menu.add_command(label="Item 1", command=menu_command)
    menu.add_command(label="Item 2", command=menu_command)
    
    # Display the menu
    root.config(menu=menu)
    
    root.mainloop()
    
  6. Customizing Tkinter Menu appearance: You can customize the appearance of a Tkinter menu by changing its font, color, and other properties.

    import tkinter as tk
    
    def menu_command():
        print("Menu item clicked")
    
    root = tk.Tk()
    
    # Create a Menu
    menu = tk.Menu(root, font=("Arial", 12), fg="blue", bg="lightgray")
    
    # Add an item to the menu with a command
    menu.add_command(label="Click Me", command=menu_command)
    
    # Display the menu
    root.config(menu=menu)
    
    root.mainloop()
    
  7. Tkinter Menu widget events and callbacks: Tkinter menu items can have associated events and callbacks. When a menu item is clicked, the associated callback function is executed.

    import tkinter as tk
    
    def menu_command():
        print("Menu item clicked")
    
    root = tk.Tk()
    
    # Create a Menu
    menu = tk.Menu(root)
    
    # Add an item to the menu with a command
    menu.add_command(label="Click Me", command=menu_command)
    
    # Display the menu
    root.config(menu=menu)
    
    root.mainloop()
    
  8. Building a multi-level menu in Tkinter: Multi-level menus in Tkinter are created using the Menu widget and add_cascade to add submenus.

    import tkinter as tk
    
    def menu_command():
        print("Menu item clicked")
    
    root = tk.Tk()
    
    # Create a Menu
    menu = tk.Menu(root)
    
    # Add an item to the menu with a command
    menu.add_command(label="Click Me", command=menu_command)
    
    # Add a submenu
    submenu = tk.Menu(menu, tearoff=0)
    submenu.add_command(label="Submenu Item 1", command=menu_command)
    submenu.add_command(label="Submenu Item 2", command=menu_command)
    menu.add_cascade(label="Submenu", menu=submenu)
    
    # Display the menu
    root.config(menu=menu)
    
    root.mainloop()