Python Tkinter Menu bar: Creating Menu items

Python tkinter Basic: Exercise-8 with Solution

Write a Python program that creates a basic menu bar with menu items using Tkinter.

Sample Solution:

Python Code:

import tkinter as tk
from tkinter import Menu

# Create the main window
parent = tk.Tk()
parent.title("Spyder (Python 3.6)")

# Create a menu bar
menu_bar = Menu(parent)

# Create a File menu
file_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="File", menu=file_menu)

# Add menu items to the File menu
file_menu.add_command(label="Exit", command=parent.quit)

# Create an Edit menu
edit_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Edit", menu=edit_menu)

# Add menu items to the Edit menu

# Create a Help menu
help_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Help", menu=help_menu)

# Add menu items to the Help menu
help_menu.add_command(label="About Spyder")

# Start the Tkinter event loop

In the exercise above we created a "Tkinter" window with a menu bar with three menus: 'File', 'Edit', and 'Help'. Each menu contains menu items. For example, the File menu has items like "New," "Open," and "Exit." Based on this basic structure, we can expand the menu items and add functionality as needed.

Sample Output:

Tkinter: Python Tkinter event handling: Button clicks. Part-1
Tkinter: Python Tkinter event handling: Button clicks. Part-2


Flowchart: Python Tkinter event handling: Button clicks.

Python Code Editor:

Previous: Python Tkinter event handling: Button clicks.
Next: Python Tkinter Messagebox: Display messages.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Summing a sequence of numbers (calculating the sum of zero to ten with skips):

>>> l = range(0,10,2)
>>> sum(l)


We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook