
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Advertisements