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 do I get the index of an item in Tkinter.Listbox?
We use the Tkinter Listbox widget to create a list of items. Each item in the listbox has sequential indexes assigned to them in vertical order starting from 0.
To get the index of a clicked item in the listbox, we use the curselection() method which returns a tuple of selected item indexes. We can then access specific items using the get() method.
Basic Example
Here's how to get the index of selected items in a Listbox ?
# Import the required libraries
from tkinter import *
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
# Create a Listbox widget
lb = Listbox(win, width=50, height=10, font=('Times', 13), selectbackground="lightblue")
lb.pack(pady=20)
# Define a function to get selected item index
def get_selection():
for item in lb.curselection():
print(f"Index: {item}, Value: {lb.get(item)}")
# Add items in the Listbox
items = ["Apple", "Banana", "Cherry", "Date", "Elderberry", "Fig"]
for item in items:
lb.insert("end", item)
# Add a Button to get selection
Button(win, text="Get Selected Index", command=get_selection, font=('Times', 12)).pack(pady=10)
win.mainloop()
Key Methods
| Method | Description | Returns |
|---|---|---|
curselection() |
Gets indexes of selected items | Tuple of integers |
get(index) |
Gets value at specific index | String value |
selection_get() |
Gets selected text directly | String value |
Multiple Selection Example
For listboxes that allow multiple selections, you can handle all selected indexes ?
from tkinter import *
win = Tk()
win.geometry("600x400")
# Create Listbox with multiple selection mode
lb = Listbox(win, selectmode=MULTIPLE, width=40, height=8)
lb.pack(pady=20)
def show_all_selections():
selected_indexes = lb.curselection()
if selected_indexes:
print("Selected items:")
for index in selected_indexes:
print(f"Index {index}: {lb.get(index)}")
else:
print("No items selected")
# Add sample items
colors = ["Red", "Green", "Blue", "Yellow", "Purple", "Orange", "Pink"]
for color in colors:
lb.insert("end", color)
Button(win, text="Show All Selections", command=show_all_selections).pack(pady=10)
win.mainloop()
Event-Driven Selection
You can also bind events to automatically detect when an item is selected ?
from tkinter import *
win = Tk()
win.geometry("500x300")
lb = Listbox(win, width=30, height=6)
lb.pack(pady=20)
def on_select(event):
selection = lb.curselection()
if selection:
index = selection[0]
value = lb.get(index)
print(f"Selected index: {index}, value: {value}")
# Bind selection event
lb.bind('<<ListboxSelect>>', on_select)
# Add items
fruits = ["Mango", "Orange", "Grapes", "Strawberry"]
for fruit in fruits:
lb.insert("end", fruit)
win.mainloop()
Conclusion
Use curselection() to get selected item indexes and get(index) to retrieve values. Bind the <<ListboxSelect>> event for automatic selection detection in interactive applications.
