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

TkinterPythonServer Side ProgrammingProgramming

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.

raja
Published on 27-Mar-2021 10:53:54
Advertisements