How to remove multiple selected items in the listbox in Tkinter?


Let us consider that we have created a listbox using the Listbox method in Tkinter and we want to remove multiple selected items from this list.

In order to select the multiple list from the Listbox, we will use selectmode as MULTIPLE. Now iterating over the list, we can perform the delete operation using some buttons.

Example

#Import the required libraries
from tkinter import *

#Create an instance of tkinter frame or window
win= Tk()

#Set the geometry
win.geometry("700x400")

#Create a text Label
label= Label(win, text="Select items from the list", font= ('Poppins bold', 18))
label.pack(pady= 20)

#Define the function
def delete_item():
   selected_item= my_list.curselection()
   for item in selected_item[::-1]:
      my_list.delete(item)

my_list= Listbox(win, selectmode= MULTIPLE)
my_list.pack()
items=['C++','java','Python','Rust','Ruby','Machine Learning']

#Now iterate over the list
for item in items:
   my_list.insert(END,item)

#Create a button to remove the selected items in the list
Button(win, text= "Delete", command= delete_item).pack()

#Keep Running the window
win.mainloop()

Output

Running the above code will produce the following output −

Now, you can select multiple entries in the listbox and click the “Delete” button to remove those entries from the list.

Observe, here we have removed three entries from the list by using the “Delete” button.

Updated on: 06-Mar-2021

496 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements