In this Python tutorial, we will learn everything about the Python Tkinter OptionMenu widget. OptionMenu widget is also called dropdown or combo box. We will be learning all the methods with examples. Also, we will cover these topics.
- Python Tkinter OptionMenu documentation
- Python Tkinter OptionMenu get value
- Python Tkinter OptionMenu width
- Python Tkinter OptionMenu style
- Python Tkinter OptionMenu command
- Python Tkinter Option set value
- Python Tkinter OptionMenu change event
- Python Tkinter OptionMenu disable
- Python Tkinter OptionMenu trace
- Python Tkinter OptionMenu color
Python Tkinter OptionMenu
- OptionMenu in Python Tkinter is used to create a drop-down menu in the application.
- It consumes less space and displays multiple options to the user. Users can select only one item out of the list of items.
- The only drawback of OptionMenu is it cannot handle large data. As the dropdown is limited to the size of the screen. So in the case of large data Listbox is used as it provides a scrolling feature into it.
- Here is the syntax of OptionMenu in Python Tkinter
OptionMenu(
master,
variable,
value,
*values,
**kwargs
)
- master is the window on which you want to place the OptionMenu. It can be the main window, secondary window or frame.
- variable means the value of the widget is not fixed, it will keep on changing. the variable can be implemented on the following:
- StringVar() – Holds a string; default value “”
- IntVar() – Holds an integer; default value 0
- DoubleVar() – Holds float value; default value 0.0
- BooleanVar() – Holds a boolean, returns 0 for False and 1 for True
- value depends upon the type of variable, if a variable is StringVar() then the value will be any name or set of characters.
- *value is the name of the list in which we have stored all the options.
- to see complete documentation run the below code.
from tkinter import *
help(OptionMenu)
Here is the preview image of OptionMenu. It is a dropdown menu displaying multiple options and user can select option of his/her choice.
Read: Registration form in Python using Tkinter
Python Tkinter OptionMenu get value
- In this section, we will learn how to get value selected by the user in Python Tkinter OptionMenu.
- OptionMenu displays multiple options but a user can select only one. So to know the option selected by a user we use the get() method.
- get() is applied on the variable assigned to the OptionMenu and it returns the currently selected option in the OptionMenu.
Example of get() method in Python Tkinter:
In this example, get() method is used to pull the information stored in the OptionMenu in Python Tkinter. Here we have mentioned the exact code to use get() method also we have created a full program based upon get() method.
def display_selected(choice):
choice = variable.get()
print(choice)
Here is the full program code to implement get() method in python tkinter.
from tkinter import *
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#F2B90C')
def display_selected(choice):
choice = variable.get()
print(choice)
countries = ['Bahamas','Canada', 'Cuba','United States']
# setting variable for Integers
variable = StringVar()
variable.set(countries[3])
# creating widget
dropdown = OptionMenu(
ws,
variable,
*countries,
command=display_selected
)
# positioning widget
dropdown.pack(expand=True)
# infinite loop
ws.mainloop()
Output:
In this output, whatever option is selected in the OptionMenu is displayed on the terminal.
This is how to get value in Python Tkinter OptionMenu.
Read: Extract text from PDF Python
Python Tkinter OptionMenu width
- In this section, we will learn how to control the width of Python Tkinter OptionMenu.
- Width is the right and left space of the widget & adjusting it according to the need of the application is important as it gives nicer look to the application.
- using the width option in the OptionMenu widget we can change the width. OptionMenu does not accept the width directly so you have to use the config method.
Example of OptionMenu Width in Python Tkinter
Here is an example of how we can change the width of OptionMenu in Python Tkinter. In this code, we have created a function that will change the width size of OptionMenu. User can choose the size from the options and OptionMenu will be changed into that size.
from tkinter import *
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#F26849')
def change_width(choice):
choice = variable.get()
dropdown.config(width=choice)
# width choices available.
width_size = [10, 15, 20, 25, 30]
# setting variable for Integers
variable = IntVar()
# creating widget
dropdown = OptionMenu(
ws,
variable,
*width_size,
command=change_width
)
# positioning widget
dropdown.pack(expand=True)
# infinite loop
ws.mainloop()
Output:
In this output, two pictures are displayed. Left picture has the width set to 10 and when the value is changed from 10 to 15 then width of the OptionMenu is also increased in the right picture.
Read: BMI Calculator Using Python Tkinter
Python Tkinter OptionMenu style
- In this section, we will learn how to apply style on OptionMenu in Python Tkinter.
- Style is the color, font, width, and other components to improve the look and feel of the OptionMenu in Python Tkinter.
Example of OptionMenu Style in Python Tkinter:
- In this output, we have increased the width and provided color to the OptionMenu in Python. Color can be changed by changing the option from OptionMenu in Python Tkinter.
- Here is the exact code that is used to change the height, width, and color of the OptionMenu in Python Tkinter. In this code dropdown is the variable assigned to OptionMenu.
dropdown.config(width=10)
dropdown.config(height=2)
dropdown.config(bg='light blue')
Here is the complete code of a program created to implement the style option in OptionMenu in Python Tkinter.
from tkinter import *
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#BF7C41')
def change_color(choice):
choice = variable.get()
dropdown.config(bg=choice)
# color choices available.
color_list = ['aquamarine', 'light blue', 'olive drab', 'gray']
# setting variable for Integers
variable = StringVar()
variable.set(color_list[1])
# creating widget
dropdown = OptionMenu(
ws,
variable,
*color_list,
command=change_color
)
dropdown.config(width=10)
dropdown.config(height=2)
dropdown.config(bg='light blue')
# positioning widget
dropdown.pack(expand=True)
# infinite loop
ws.mainloop()
Output:
In this output, the color of OptionMenu changes with the change in options. Also, the height and width applied to it is giving good look to the entire application.
Read: Python Tkinter Title
Python Tkinter OptionMenu command
- In this section, we will learn the need and use of the Python Tkinter OptionMenu command.
- A command is used to assign a callback function to the OptionMenu. It allows you to trigger a function if a particular option is selected in the OptionMenu.
Example of OptionMenu Command in Python Tkinter:
In this example, the user has the choice to select the application color from the OptionMenu. The window background will be changed in the selected color.
from tkinter import *
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#F26849')
def change_color(choice):
choice = variable.get()
ws.config(bg=choice)
# color choices available.
color_list = ['red', 'green', 'yellow', 'blue', 'pink']
# setting variable for Integers
variable = StringVar()
# creating widget
dropdown = OptionMenu(
ws,
variable,
*color_list,
command=change_color
)
# positioning widget
dropdown.pack(expand=True)
# infinite loop
ws.mainloop()
Output:
Here is the output of the above code, you can see that the color of window can be changed by selecting any colors from the OptionMenu in Python Tkinter.
Read: Python QR code generator using pyqrcode in Tkinter
Python Tkinter Option set value
- Python Tkinter OptionMenu offers a method set() that allows setting the default value in OptionMenu.
- set() method can be applied on the variable and it requires the item from the list that you want to display as default.
Example of OptionMenu command with set value option.:
In this example, we have set the default country as ‘United States. So if the user will not select any option the default value will be taken as united states in OptionMenu.
from tkinter import *
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#597678')
# choices available with user.
countries = ['Bahamas','Canada', 'Cuba', 'Dominica', 'Jamaica', 'Mexico', 'United States']
# setting variable for strings
variable = StringVar()
# set default country as United States
variable.set(countries[6])
# creating widget
dropdown = OptionMenu(
ws,
variable,
*countries
)
# positioning widget
dropdown.pack(expand=True)
# infinite loop
ws.mainloop()
Output:
In this output, the picture on the left shows the OptionMenu when no default value assigned while the picture on the right shows the auto-selected ‘United States. So it is done using a set() method in Python Tkinter.
Read: How to Set Background to be an Image in Python Tkinter
Python Tkinter OptionMenu change event
Event is the change the occurred in response to the activity performed. In OptionMenu every time the option is changed it is called an event. Using variable we can get the information about the changes occurred.
Example of OptionMenu change event in Python Tkinter:
In this example, changes will be displayed on the terminal. Below mentioned code is responsible for reporting all the changing events.
def check(*args):
print(f"the variable has changed to '{variable.get()}'")
variable = StringVar(value='United States')
variable.trace('w', check)
Here is the full code for the program that will display the changed event logs in Python Tkinter.
from tkinter import *
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#597678')
def check(*args):
print(f"the variable has changed to '{variable.get()}'")
variable = StringVar(value='United States')
variable.trace('w', check)
# choices available with user.
countries = ['Bahamas','Canada', 'Cuba', 'Dominica', 'Jamaica', 'Mexico', 'United States']
# set default country as United States
variable.set(countries[6])
# creating widget
dropdown = OptionMenu(
ws,
variable,
*countries
)
# positioning widget
dropdown.pack(expand=True)
# infinite loop
ws.mainloop()
Output:
In this output, every time option is changed in the OptionMenu, a message is reported on the terminal screen.
Read: Python Tkinter to Display Data in Textboxes
Python Tkinter OptionMenu disable
- Python Tkinter OptionMenu provides a method state using which OptionMenu can be greyed out. Once greyed out all the functionality of OptionMenu is disabled. No changes will take place when clicked, no dropdown will be displayed.
- The state provides two options:
- disabled – It disables the OptionMenu widget in Python Tkinter.
- normal – It enables the OptionMenu widget in Python Tkinter
Example of OptionMenu disable in Python Tkinter:
In this example, we have created a checkbox and set the rules that if the checkbox is checked then the OptionMenu is in normal state and if checkbox is unchecked or deselected then OptionMenu will change to disabled state.
This the function that is responsible for the change in state of OptionMenu widget in Python Tkinter.
def changeState():
if checkVar.get() == 1:
dropdown.configure(state='normal')
else:
dropdown.configure(state='disabled')
Here is the full code for the program.
from tkinter import *
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#393C73')
def changeState():
if checkVar.get() == 1:
dropdown.configure(state='normal')
else:
dropdown.configure(state='disabled')
variable = StringVar()
checkVar = IntVar()
checkVar.set(1)
# checkbutton widget
cb = Checkbutton(
ws,
text = "Enable/Disable OptionMenu",
variable = checkVar,
onvalue = 1,
offvalue = 0,
height=2,
width=30,
command=changeState
)
cb.pack(expand=True)
# choices available with user.
countries = ['Bahamas','Canada', 'Cuba', 'Dominica', 'Jamaica', 'Mexico', 'United States']
# set default country as United States
variable.set(countries[6])
# creating widget
dropdown = OptionMenu(
ws,
variable,
*countries
)
# positioning widget
dropdown.pack(expand=True)
# infinite loop
ws.mainloop()
Output:
In the output, in the left picture checkbox is selected or check and user is able is click and operate the OptionMenu widget in Python Tkinter. But in the right image checkbox is disable and now OptionMenu is greyed out. Select the checkbox again to bring OptionMenu back in working state.
Read: How to Create Countdown Timer using Python Tkinter
Python Tkinter OptionMenu trace
Trace is similar to the change event section, Trace simply means to track the changes that occurred in the OptionMenu widget in Python Tkinter.
Example of OptionMenu change event in Python Tkinter:
In this example, changes will be displayed on the terminal. Below mentioned code is responsible for tracking and reporting all the changes that occurred in the OptionMenu widget in Python Tkinter.
def check(*args):
print(f"the variable has changed to '{variable.get()}'")
variable = StringVar(value='United States')
variable.trace('w', check)
Here is the full code to implement trace in OptionMenu widget in Python Tkinter.
from tkinter import *
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#597678')
def check(*args):
print(f"the variable has changed to '{variable.get()}'")
variable = StringVar(value='United States')
variable.trace('w', check)
# choices available with user.
countries = ['Bahamas','Canada', 'Cuba', 'Dominica', 'Jamaica', 'Mexico', 'United States']
# set default country as United States
variable.set(countries[6])
# creating widget
dropdown = OptionMenu(
ws,
variable,
*countries
)
# positioning widget
dropdown.pack(expand=True)
# infinite loop
ws.mainloop()
Output:
In this output, every time option is changed in the OptionMenu, a message is reported on the terminal screen.
Python Tkinter OptionMenu color
In this section, we will see how to apply color on OptionMenu in Python Tkinter. Applying color is a part of styling a widget.
Example of OptionMenu Color in Python Tkinter:
In this example, color of the OptionMenu widget can be changed by changing the option from OptionMenu widget in Python Tkinter.
from tkinter import *
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#BF7C41')
def change_color(choice):
choice = variable.get()
dropdown.config(bg=choice)
# color choices available.
color_list = ['aquamarine', 'light blue', 'olive drab', 'gray']
# setting variable for Integers
variable = StringVar()
variable.set(color_list[1])
# creating widget
dropdown = OptionMenu(
ws,
variable,
*color_list,
command=change_color
)
dropdown.config(width=10)
dropdown.config(height=2)
dropdown.config(bg='light blue')
# positioning widget
dropdown.pack(expand=True)
# infinite loop
ws.mainloop()
Output:
In this output, the color of OptionMenu changes with the change in options. Also, the height and width applied to it is giving good look to the entire application.
You may like:
- Upload a File in Python Tkinter
- Python Tkinter drag and drop
- Python Tkinter Map() Function
- Python Tkinter Grid
In this tutorial, we have learned everything about OptionMenu widget in Python Tkinter. Also, we have covered these topics.
- Python Tkinter OptionMenu
- Python Tkinter OptionMenu get value
- Python Tkinter OptionMenu width
- Python Tkinter OptionMenu style
- Python Tkinter OptionMenu command
- Python Tkinter OptionMenu style
- Python Tkinter Option set value
- Python Tkinter OptionMenu change event
- Python Tkinter OptionMenu disable
- Python Tkinter OptionMenu trace
- Python Tkinter OptionMenu color
- Python Tkinter OptionMenu documentation
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.