How can I create a dropdown menu from a List in Tkinter?


Let us suppose we want to create a dropdown menu of a list in an application using tkinter. In this case, we can use the Tkinter OptionMenu(win, menu_to_set, options) function.

First, we will instantiate an object of StringVar(), then we will set the initial value of the dropdown menu. We will create the dropdown menu by creating an object of OptionMenu and passing the value of window, menu object, and options which are to be displayed.

Example

#Import the required libraries
from tkinter import *

#Create an instance of tkinter frame
win= Tk()

#Define the size of window or frame
win.geometry("715x250")

#Set the Menu initially
menu= StringVar()
menu.set("Select Any Language")

#Create a dropdown Menu
drop= OptionMenu(win, menu,"C++", "Java","Python","JavaScript","Rust","GoLang")
drop.pack()

win.mainloop()

Output

In the output window, you can select an option by clicking over “Select Any Language” and it will show a list in the dropdown menu.

Updated on: 22-Apr-2021

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements