Topic: Adding Menus to Python 3 tkinter GUI Programs
Abou the GUI Program with Menu
This is a Python 3 GUI program to show the use of Menu in GUI Python programs. It is written using tkinter module. It shows a menu bar with two menus. File menu has options of New, OPen, Save and Exit. Edit menu will show three options of Cut, Copy and Paste.
Whenever a user selects any option from a menu, respective label of the option will be displayed in the title bar of the main window of the program.
The ‘Exit ‘ option will terminate the execution of the program by executing the statement
main_window.destroy()
How This GUI Program with tkinter Menu Works
This Python Program is self explanatory because of the addition of proper comments almost for each and every line of code.
Adding Python 3 tkinter Menus in GUI Programs
# Python Menu Program to show # File and Edit Menue # When an option clicked # Option Label is shown in title of # application window from tkinter import * # Menu click handling functions for File menu def new(): main_window.title("'New' is selected") def open1(): main_window.title ("'Open' is selected") def save(): main_window.title("'Save' is selected") def exit1(): main_window.destroy() # Menu click handling functions for Edit menu def cut(): main_window.title("'Cut' is selected") def copy(): main_window.title("'Copy' is selected") def paste(): main_window.title("'Paste' is selected") # create main window main_window = Tk() # set size and position of main window main_window.geometry("300x300+550+350") # create menu bar menubar = Menu(main_window) # create file menu file = Menu(menubar, tearoff = 0) # create file menu commands New,Open,Save,Exit file.add_command(label="New", command = new) file.add_command(label = "Open", command = open1) file.add_command(label = "Save", command = save) file.add_command(label = "Exit", command = exit1) # add file menu to menu bar menubar.add_cascade(label = "File", menu = file) # create edit menu edit = Menu(menubar, tearoff=0) # create edit menu commands Cut,Copy,Paste edit.add_command(label = "Cut", command = cut) edit.add_command(label = "Copy", command = copy) edit.add_command(label = "Paste", command = paste) # add edit menu to menu bar menubar.add_cascade(label = "Edit", menu = edit) # attach menubar to main window main_window.config(menu = menubar) # start mainloop(), start GUI main_window.mainloop()
You may also like the following Python 3 tkinter GUI Programs
- Explain Python grid Geometry Manager with Example Code
- Miles to Kilometers Converter tkinter GUI Program
- Python 3 tkinter GUI Kilogram to Pounds Program
- Python Set Label Text on Button Click tkinter GUI Program
- Show Label and Button Widgets Python Tkinter Program
- GUI Hello World Program in Python 3
- Create and Show Root Window of tkinter GUI Program
Pingback: Python GUI Find Factorial by Recursive Function | EasyCodeBook.com
Pingback: Python Quotes Changer Program tkinter GUI | EasyCodeBook.com