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 get the index of selected option in Tkinter Combobox?
The Tkinter Combobox widget allows you to create dropdown lists where users can select items. To get the index of the selected option, you can use the current() method, which returns the zero-based index of the selected item.
Syntax
combobox.current()
The current() method returns an integer representing the index position of the selected item in the combobox values list.
Example
Here's a complete example that demonstrates how to get the index of selected option in a Tkinter Combobox ?
import tkinter as tk
from tkinter import ttk
# Create main window
root = tk.Tk()
root.title("Combobox Index Example")
root.geometry("400x200")
# Define list of days
days = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')
# Create StringVar for combobox
selected_var = tk.StringVar()
# Function to display selected index
def on_selection(event):
selected_index = combobox.current()
selected_value = selected_var.get()
result_label.config(text=f"Index: {selected_index}, Value: {selected_value}")
# Create combobox
combobox = ttk.Combobox(root, textvariable=selected_var, values=days, state="readonly")
combobox.pack(pady=20)
combobox.bind("<<ComboboxSelected>>", on_selection)
# Label to show results
result_label = tk.Label(root, text="Select a day to see its index",
font=("Arial", 12))
result_label.pack(pady=20)
# Clear button
def clear_selection():
combobox.set('')
result_label.config(text="Select a day to see its index")
clear_button = tk.Button(root, text="Clear", command=clear_selection)
clear_button.pack(pady=10)
root.mainloop()
Alternative Method Using trace()
You can also use the trace() method to automatically detect when the selection changes ?
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Combobox with trace()")
root.geometry("400x150")
# List of colors
colors = ['Red', 'Green', 'Blue', 'Yellow', 'Orange', 'Purple']
# StringVar with trace
selected_var = tk.StringVar()
# Trace function
def on_change(*args):
if selected_var.get(): # Only if something is selected
index = combobox.current()
info_label.config(text=f"Selected: {selected_var.get()} (Index: {index})")
selected_var.trace('w', on_change)
# Create combobox
combobox = ttk.Combobox(root, textvariable=selected_var,
values=colors, state="readonly")
combobox.pack(pady=20)
# Info label
info_label = tk.Label(root, text="No selection", font=("Arial", 11))
info_label.pack(pady=10)
root.mainloop()
Key Methods
| Method | Description | Return Value |
|---|---|---|
current() |
Get index of selected item | Integer (0-based index) |
get() |
Get value of selected item | String (selected value) |
set('') |
Clear the selection | None |
Conclusion
Use current() to get the zero-based index of the selected combobox item. Combine it with event binding or variable tracing to create responsive dropdown interfaces that react to user selections.
