Thursday, December 26, 2024
Google search engine
HomeLanguagesDropdown Menus – Tkinter

Dropdown Menus – Tkinter

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:

  1. Define the datatype of menu text, means integer, string, or any other datatype
  2. Set initial menu text (That display initially)
  3. Add menu value in option as a list
  4. Create Dropdown menu

Below is an Implementation that creates Dropdown menus in Tkinter:

Python3




# Import module
from tkinter import *
  
# Create object
root = Tk()
  
# Adjust size
root.geometry( "200x200" )
  
# Change the label text
def show():
    label.config( text = clicked.get() )
  
# Dropdown menu options
options = [
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday"
]
  
# datatype of menu text
clicked = StringVar()
  
# initial menu text
clicked.set( "Monday" )
  
# Create Dropdown menu
drop = OptionMenu( root , clicked , *options )
drop.pack()
  
# Create button, it will change label text
button = Button( root , text = "click Me" , command = show ).pack()
  
# Create Label
label = Label( root , text = " " )
label.pack()
  
# Execute tkinter
root.mainloop()


Output:-

RELATED ARTICLES

Most Popular

Recent Comments