Dropdown Menus – Tkinter
Last Updated :
26 Nov, 2020
Prerequisite: Python GUI – tkinter
Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.
OptionMenu
OptionMenu is an important part of any GUI. It creates a popup menu, and a button to display it. It is similar to the combobox widgets commonly used on Windows.
Syntax:
OptionMenu(master,options)
Parameters:
- master: This parameter is used to represents the parent window.
- options: Contain the Menu values
For creating Dropdown menu follow these steps:
- Define the datatype of menu text, means integer, string, or any other datatype
- Set initial menu text (That display initially)
- Add menu value in option as a list
- Create Dropdown menu
Below is an Implementation that creates Dropdown menus in Tkinter:
Python3
from tkinter import *
root = Tk()
root.geometry( "200x200" )
def show():
label.config( text = clicked.get() )
options = [
"Monday" ,
"Tuesday" ,
"Wednesday" ,
"Thursday" ,
"Friday" ,
"Saturday" ,
"Sunday"
]
clicked = StringVar()
clicked. set ( "Monday" )
drop = OptionMenu( root , clicked , * options )
drop.pack()
button = Button( root , text = "click Me" , command = show ).pack()
label = Label( root , text = " " )
label.pack()
root.mainloop()
|
Output:-
Share your thoughts in the comments
Please Login to comment...