Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Tkinter Menus and Toolbars


Creating menus and toolbars in Tkinter is essential for building a functional and user-friendly GUI application. Here's a step-by-step guide to creating menus and toolbars in Tkinter:

1. Creating Menus

Menus are usually placed at the top of the window and can contain various commands and options.

Example: Creating a Basic Menu


python
                        import tkinter as tk

                        def say_hello():
                            print("Hello!")

                        # Create the main window
                        root = tk.Tk()
                        root.title("Menu Example")
                        root.geometry("400x300")

                        # Create a menu bar
                        menu_bar = tk.Menu(root)

                        # Create a File menu
                        file_menu = tk.Menu(menu_bar, tearoff=0)
                        file_menu.add_command(label="New", command=say_hello)
                        file_menu.add_command(label="Open", command=say_hello)
                        file_menu.add_separator()
                        file_menu.add_command(label="Exit", command=root.quit)

                        # Add the File menu to the menu bar
                        menu_bar.add_cascade(label="File", menu=file_menu)

                        # Create an Edit menu
                        edit_menu = tk.Menu(menu_bar, tearoff=0)
                        edit_menu.add_command(label="Cut", command=say_hello)
                        edit_menu.add_command(label="Copy", command=say_hello)
                        edit_menu.add_command(label="Paste", command=say_hello)

                        # Add the Edit menu to the menu bar
                        menu_bar.add_cascade(label="Edit", menu=edit_menu)

                        # Display the menu bar
                        root.config(menu=menu_bar)

                        # Run the application
                        root.mainloop()

                    

Explanation:

  • tk.Menu(root): Creates a menu bar.
  • tk.Menu(menu_bar, tearoff=0): Creates a menu (e.g., File, Edit) and disables the tear-off feature.
  • file_menu.add_command(label="New", command=say_hello): Adds a command to the menu.
  • menu_bar.add_cascade(label="File", menu=file_menu): Adds the File menu to the menu bar.
  • root.config(menu=menu_bar): Displays the menu bar in the main window.


2. Adding Submenus

Submenus are menus within menus, useful for organizing related commands.

Example: Adding Submenus


python
                        import tkinter as tk

                        def say_hello():
                            print("Hello!")

                        # Create the main window
                        root = tk.Tk()
                        root.title("Submenu Example")
                        root.geometry("400x300")

                        # Create a menu bar
                        menu_bar = tk.Menu(root)

                        # Create a File menu
                        file_menu = tk.Menu(menu_bar, tearoff=0)
                        file_menu.add_command(label="New", command=say_hello)
                        file_menu.add_command(label="Open", command=say_hello)

                        # Create a Recent Files submenu
                        recent_files_menu = tk.Menu(file_menu, tearoff=0)
                        recent_files_menu.add_command(label="File1", command=say_hello)
                        recent_files_menu.add_command(label="File2", command=say_hello)

                        # Add the Recent Files submenu to the File menu
                        file_menu.add_cascade(label="Recent Files", menu=recent_files_menu)

                        file_menu.add_separator()
                        file_menu.add_command(label="Exit", command=root.quit)

                        # Add the File menu to the menu bar
                        menu_bar.add_cascade(label="File", menu=file_menu)

                        # Display the menu bar
                        root.config(menu=menu_bar)

                        # Run the application
                        root.mainloop()

                    

Explanation:

  • recent_files_menu = tk.Menu(file_menu, tearoff=0): Creates a submenu.
  • file_menu.add_cascade(label="Recent Files", menu=recent_files_menu): Adds the Recent Files submenu to the File menu.


3. Creating Context Menus

Context menus are pop-up menus that appear on right-click events.

Example: Creating a Context Menu


python
                        import tkinter as tk

                        def say_hello():
                            print("Hello!")

                        # Create the main window
                        root = tk.Tk()
                        root.title("Context Menu Example")
                        root.geometry("400x300")

                        # Create a context menu
                        context_menu = tk.Menu(root, tearoff=0)
                        context_menu.add_command(label="Cut", command=say_hello)
                        context_menu.add_command(label="Copy", command=say_hello)
                        context_menu.add_command(label="Paste", command=say_hello)

                        # Function to show the context menu
                        def show_context_menu(event):
                            context_menu.post(event.x_root, event.y_root)

                        # Bind the right-click event to the function
                        root.bind("<  >", show_context_menu)
                        
                        # Run the application
                        root.mainloop()
                        
                    

Explanation:

  • context_menu.post(event.x_root, event.y_root): Displays the context menu at the cursor's position.


4. Creating Toolbars

Toolbars are usually placed at the top of the window, below the menu bar, and contain buttons for quick access to commands.

Example: Creating a Basic Toolbar


python
                        import tkinter as tk

                        def say_hello():
                            print("Hello!")

                        # Create the main window
                        root = tk.Tk()
                        root.title("Toolbar Example")
                        root.geometry("400x300")

                        # Create a toolbar frame
                        toolbar = tk.Frame(root, bd=1, relief=tk.RAISED)
                        toolbar.pack(side=tk.TOP, fill=tk.X)

                        # Create buttons for the toolbar
                        new_button = tk.Button(toolbar, text="New", command=say_hello)
                        new_button.pack(side=tk.LEFT, padx=2, pady=2)

                        open_button = tk.Button(toolbar, text="Open", command=say_hello)
                        open_button.pack(side=tk.LEFT, padx=2, pady=2)

                        save_button = tk.Button(toolbar, text="Save", command=say_hello)
                        save_button.pack(side=tk.LEFT, padx=2, pady=2)

                        # Run the application
                        root.mainloop()

                    

Explanation:

  • toolbar = tk.Frame(root, bd=1, relief=tk.RAISED): Creates a toolbar frame with a raised border.
  • toolbar.pack(side=tk.TOP, fill=tk.X): Places the toolbar at the top of the window and makes it fill the width of the window.
  • tk.Button(toolbar, text="New", command=say_hello).pack(side=tk.LEFT, padx=2, pady=2): Adds buttons to the toolbar.

Summary

By following these steps, you can effectively create menus and toolbars in Tkinter. Menus allow you to organize commands and options in a structured way, while toolbars provide quick access to frequently used commands. Using these elements enhances the functionality and user experience of your Tkinter applications.







Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java