Python GUI Program: Creating a menu bar with Tkinter
Write a Python GUI program to create a Menu bar with File, Edit, and Help menus, each containing submenu items using tkinter module.
Sample Solution:
Python Code:
Explanation:
In the exercise above -
- Import the required libraries, including tkinter for GUI elements and "Menu" for creating menus.
- Define several functions ("new_file()", "open_file()", "save_file()", "cut_text()", "copy_text()", "paste_text()", and "about()") that will be executed when submenu items are clicked. You can replace print statements with actual functionality.
- Create the main window using tk.Tk() and set its title.
- Create a menu bar using Menu(root) and set it as the main menu using root.config(menu=menu_bar).
- Create three menus: 'File', 'Edit', and 'Help', using Menu(menu_bar, tearoff=0). The tearoff option specifies whether the menu can be torn off as a separate window (0 means no).
- Add submenu items to each menu using menu.add_command(label="Item Label", command=callback_function). Label specifies the item's label, and command specifies the action to take when the item is clicked.
- Use menu_bar.add_cascade(label="Menu Name", menu=menu) to add each menu to the menu bar.
- The main event loop, root.mainloop(), starts the GUI application.
Sample Output:
New File Cut Text About this application![]()
Flowchart:


Go to:
Previous: Building a Hierarchical list widget.
Next: Font size control with Tkinter scale widget.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.