- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 some indexes that are assigned to them sequentially in vertical order.
Let us suppose that we want to get the index of a clicked item in the listbox. Then, we have to first create a button that will capture the current selection of the items using list.curselection() method and then, we will print the index using the get() method.
# 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=100, height=10, font=('Times 13'), selectbackground="black") lb.pack() # Define a function to edit the listbox ite def save(): for item in lb.curselection(): print("You have selected " + str(item+1)) # Add items in the Listbox lb.insert("end", "A", "B", "C", "D", "E", "F") # Add a Button To Edit and Delete the Listbox Item Button(win, text="Save", command=save).pack() win.mainloop()
If we run the above code, it will display a window containing a list of alphabets (A-F).
Select an item from the list and click the "Save" button to get the index of the selected item printed on the console.
You have selected 3
- How do you get the index of an element in a list in Java?
- How do I get the src of an image in Selenium?
- How do I insert an item between two items in a list in Java?
- How do I find out the size of a canvas item in Tkinter?
- How to find the index of an item given a list containing it in Python?
- How do I get the resource id of an image if I know its name in Android?
- How to find the index of an item in a C# list in a single step?
- How do I hide and show a menu item in the Android ActionBar?
- How can I delete an item from an Object in MongoDB?
- How to remove an item from a C# list by using an index?
- How to insert an item into a C# list by using an index?
- How do I get the resource id of an image if I know its name in Android using Kotlin?
- How do I get the parent directory in Python?