How to Create an OptionMenu in Python Tkinter?

As a developer working on a project for one of my clients, I recently faced the challenge of creating a user-friendly form that required a dropdown menu. After researching various options, I found that the Tkinter OptionMenu widget was the perfect solution. In this article, I will explain how to create an optionmenu in Python Tkinter and share my experience, and guide you through the process of implementing an OptionMenu in your application.

Tkinter OptionMenu Widget

The Tkinter OptionMenu widget is a dropdown menu that displays a list of options for the user to choose from. It provides a convenient way to present a set of predefined choices without cluttering the user interface. When the user clicks on the OptionMenu, it expands to show the available options, and the selected option is displayed on the widget.

Here’s a simple example of creating an OptionMenu in Tkinter:

from tkinter import *

window = Tk()
window.title("OptionMenu Example")

states = ["California", "New York", "Texas", "Florida", "Illinois"]

selected_state = StringVar()
selected_state.set(states[0])

state_dropdown = OptionMenu(window, selected_state, *states)
state_dropdown.pack()

window.mainloop()

You can look at the output in the screenshot below.

Create an OptionMenu in Python Tkinter

In this example, we create a list called states that contains the names of some US states. We then create a StringVar called selected_state to hold the user’s selection and set its initial value to the first state in the list. Finally, we create the OptionMenu widget, passing the window, selected_state, and the list of states as arguments.

Read How to Create a Text Box in Python Tkinter?

Customize the OptionMenu Appearance

By default, the OptionMenu widget has a simple appearance. However, you can customize its look and feel to match your application’s design. Here are a few ways to modify the OptionMenu’s appearance:

  1. Width: You can set the width of the OptionMenu using the width parameter. For example:
state_dropdown.config(width=15)

You can look at the output in the screenshot below.

How to Create an OptionMenu in Python Tkinter
  1. Background Color: To change the background color of the OptionMenu, use the bg parameter:
state_dropdown.config(bg="lightblue")

You can look at the output in the screenshot below.

Create an OptionMenu in Python Tkinter background color
  1. Text Color: To modify the color of the text inside the OptionMenu, use the fg parameter:
state_dropdown.config(fg="navy")

You can look at the output in the screenshot below.

Create an OptionMenu in Python Tkinter text color
  1. Font: You can specify the font style and size using the font parameter:
state_dropdown.config(font=("Arial", 12))

You can look at the output in the screenshot below.

Create an OptionMenu in Python Tkinter font

By combining these customization options, you can create an OptionMenu that seamlessly integrates with your application’s visual style.

Check out How to Create Labels in Python with Tkinter?

Create an OptionMenu in Python Tkinter

Let us see how we can create an optionmenu by applying different functionalities.

1. Retrieve the Selected Options

Once the user selects an option from the OptionMenu, you’ll likely want to retrieve the selected value for further processing. Here’s how you can access the selected option:

def get_selected_state():
    selected = selected_state.get()
    print("Selected State:", selected)

submit_button = Button(window, text="Submit", command=get_selected_state)
submit_button.pack()

You can look at the output in the screenshot below.

Create an OptionMenu in Python Tkinter get selected option

In this example, we define a function called get_selected_state() that retrieves the value of the selected_state variable using the get() method. We then create a “Submit” button and associate it with the get_selected_state() function using the command parameter. When the user clicks the “Submit” button, the selected state will be printed to the console.

Read How to Create Buttons in Python with Tkinter?

2. 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.

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()

You can look at the output in the screenshot below.

Create an OptionMenu in Python Tkinter command

The color of the window can be changed by selecting any colors from the OptionMenu in Python Tkinter.

Check out How to Create a Menu Bar in Tkinter?

3. Option set value

set() method can be applied to the variable and it requires the item from the list that you want to display as default.

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()

You can look at the output in the screenshot below.

Create an OptionMenu in Python Tkinter default

OptionMenu when no default value is assigned shows the auto-selected ‘United States’. So it is done using a set() method in Python Tkinter.

Read How to Use Tkinter Entry Widget in Python?

4. 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

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()

If the checkbox is selected then the user is able is click and operate the OptionMenu widget in Python Tkinter, if the checkbox is disabled then OptionMenu is greyed out. Select the checkbox again to bring OptionMenu back to a working state.

Check out How to Create Checkboxes in Python Tkinter?

Conclusion

In this tutorial, I have explained how to create an optionmenu in Python Tkinter. I discussed how to customize the optionmenu appearance by considering width, font, background color, text color, etc. I also discussed creating optionmenu with functionalities like retrieving selected options, command, setting value, and disabling optionmenu.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.