Tkinter ListBox Delete specific index items
If the delete() method contains a parameter, it means delete this index item.
Tkinter ListBox Delete specific index items Example#1
After creating the Listbox and deleting the item with index 1, the original item with index 1 is Watermelon, which will not be displayed after execution because it has been deleted.
from tkinter import *
fruits = [
"Banana","Watermelon","Pineapple",
"Orange","Grapes","Mango"
]
root = Tk()
root.title("apidemos.com")
root.geometry("300x210")
lb = Listbox(root)
for fruit in fruits:
lb.insert(END,fruit)
lb.pack(pady=10)
lb.delete(1)
root.mainloop()
Output:
If there are two arguments within the delete() method, it means the delete interval option, the first argument is the starting index item of the interval and the second argument is the ending index item of the interval.
Tkinter ListBox Delete specific index items Example#2
Create a Listbox, and then delete the items with indexes 1 to 3.
from tkinter import *
fruits = [
"Banana","Watermelon","Pineapple",
"Orange","Grapes","Mango"
]
root = Tk()
root.title("apidemos.com")
root.geometry("300x210")
lb = Listbox(root)
for fruit in fruits:
lb.insert(END,fruit)
lb.pack(pady=10)
lb.delete(1,3)
root.mainloop()
Output: