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 directly modify a specific item in a TKinter listbox?
Tkinter is a Python-based GUI application development library which is generally used to build useful functional desktop applications. The Listbox widget is another tkinter widget, which is used as a container to display a list of items in the form of a Listbox.
To define a list of items in a Listbox widget, you'll need to create a constructor of Listbox(root, width, height, **options). You can insert as many items as you want to display in the listbox.
To modify a specific item in a tkinter Listbox, you can first select the item from the list you want to modify, then call the delete() method to delete the existing value, and finally use insert() to add a new item at that position.
Syntax
# Delete an item at specific index listbox.delete(index) # Insert an item at specific position listbox.insert(index, "new_value")
Example
Let's create a complete example that demonstrates how to modify a specific item in a Listbox ?
# Import the required libraries
from tkinter import *
from tkinter import ttk
# 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, background="purple3",
foreground="white", font=('Times 13'), selectbackground="white")
lb.pack()
# Function to edit the currently selected item
def edit_current():
for item in lb.curselection():
lb.delete(item)
lb.insert(item, "Modified Item")
# Add items in the Listbox
lb.insert("end", "item1", "item2", "item3", "item4", "item5")
# Add a Button to Edit the selected Listbox Item
ttk.Button(win, text="Edit Selected", command=edit_current).pack()
win.mainloop()
How It Works
The edit_current() function performs the following steps:
-
lb.curselection()returns a tuple of indices of currently selected items -
lb.delete(item)removes the item at the specified index -
lb.insert(item, "Modified Item")inserts a new value at the same position
Alternative Approach - Using Entry Widget
For more dynamic modifications, you can combine a Listbox with an Entry widget ?
from tkinter import *
from tkinter import ttk
win = Tk()
win.geometry("700x400")
# Create Listbox
lb = Listbox(win, width=50, height=8)
lb.pack(pady=10)
# Create Entry widget for new value
entry = Entry(win, width=50)
entry.pack(pady=5)
def modify_selected():
selection = lb.curselection()
if selection:
index = selection[0]
new_value = entry.get()
if new_value:
lb.delete(index)
lb.insert(index, new_value)
entry.delete(0, END)
# Add sample items
items = ["Apple", "Banana", "Orange", "Grapes", "Mango"]
for item in items:
lb.insert(END, item)
# Create modify button
ttk.Button(win, text="Modify Selected", command=modify_selected).pack(pady=5)
win.mainloop()
Key Points
- Always check if an item is selected using
curselection()before modifying - Use
delete()followed byinsert()at the same index to replace an item - The
curselection()method returns a tuple of selected indices - You can modify multiple selected items by iterating through the selection
Conclusion
Modifying Listbox items requires deleting the existing item and inserting a new one at the same position. Use curselection() to get selected indices and combine delete() with insert() for seamless item modification.
