Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to call a function using the OptionMenu widget in Tkinter?
The OptionMenu widget in Tkinter allows users to select from a dropdown list of options. You can call a function when an option is selected by using the command parameter and a StringVar object to track the selected value.
Basic Setup
First, create a StringVar object to manage the selected value and define a callback function ?
from tkinter import *
# Create main window
root = Tk()
root.geometry("400x300")
root.title("OptionMenu Example")
# Create StringVar to store selected value
selected_city = StringVar()
selected_city.set("Select a City")
# Define callback function
def on_city_select(value):
result_label.config(text=f"You selected: {value}")
# Create label to display selection
result_label = Label(root, text="No city selected", font=("Arial", 12))
result_label.pack(pady=20)
root.mainloop()
Complete Example with OptionMenu
Here's a complete example showing how to create an OptionMenu and call a function when an option is selected ?
from tkinter import *
# Create main window
root = Tk()
root.geometry("400x300")
root.title("City Selection")
# Callback function for OptionMenu selection
def city_selected(selected_value):
result_label.config(text=f"You have selected: {selected_value}")
print(f"User selected: {selected_value}")
# Create StringVar to hold the selected value
city_var = StringVar()
city_var.set("Choose a city")
# List of cities for the OptionMenu
cities = ["Mumbai", "Delhi", "Bangalore", "Chennai", "Kolkata", "Hyderabad"]
# Create OptionMenu widget
city_menu = OptionMenu(root, city_var, *cities, command=city_selected)
city_menu.config(width=20, font=("Arial", 10))
city_menu.pack(pady=30)
# Label to display the selection
result_label = Label(root, text="No city selected yet",
font=("Arial", 12), fg="blue")
result_label.pack(pady=20)
# Start the GUI event loop
root.mainloop()
Key Components
StringVar() − Manages the selected value from the OptionMenu
command parameter − Specifies the function to call when selection changes
*cities − Unpacks the list to provide options for the menu
Callback function − Receives the selected value as a parameter
Alternative Approach Using trace()
You can also use the trace() method to monitor changes to the StringVar ?
from tkinter import *
root = Tk()
root.geometry("400x200")
def on_selection_change(*args):
selected = city_var.get()
status_label.config(text=f"Current selection: {selected}")
# Create StringVar and set up tracing
city_var = StringVar()
city_var.set("Select option")
city_var.trace('w', on_selection_change) # 'w' for write operations
# Create OptionMenu without command parameter
cities = ["Tokyo", "Paris", "London", "New York"]
OptionMenu(root, city_var, *cities).pack(pady=20)
status_label = Label(root, text="Waiting for selection...")
status_label.pack(pady=10)
root.mainloop()
Comparison
| Method | When Triggered | Parameters Received |
|---|---|---|
command |
Option selected | Selected value |
trace() |
StringVar changes | Variable name, index, mode |
Conclusion
Use the command parameter for simple callbacks when options are selected. The trace() method provides more control for monitoring StringVar changes in complex applications.
