Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 9 - GUI Programming using Tkinter - Section 9.10 - Popup Menus - Check Point - MyProgrammingLab - Page 294: 9.28

Answer

In Tkinter, you can display a menu bar by creating an instance of the Menu widget and then adding it to your main window using the config method.

Work Step by Step

Here's an example: import tkinter as tk root = tk.Tk() menubar = tk.Menu(root) filemenu = tk.Menu(menubar, tearoff=0) filemenu.add_command(label="Open", command=lambda: print("Open")) filemenu.add_command(label="Save", command=lambda: print("Save")) filemenu.add_separator() filemenu.add_command(label="Exit", command=root.quit) menubar.add_cascade(label="File", menu=filemenu) editmenu = tk.Menu(menubar, tearoff=0) editmenu.add_command(label="Cut", command=lambda: print("Cut")) editmenu.add_command(label="Copy", command=lambda: print("Copy")) editmenu.add_command(label="Paste", command=lambda: print("Paste")) menubar.add_cascade(label="Edit", menu=editmenu) root.config(menu=menubar) root.mainloop() This code creates a Tk object that represents the main window, creates a Menu widget to serve as the menu bar, creates two sub-menus for "File" and "Edit", and adds various commands to each sub-menu. Finally, the menu bar is added to the main window using the config method.
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.