- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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.
#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()
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.
- Related Articles
- How to edit a Listbox item in Tkinter?
- How to correctly select multiple items with the mouse in Tkinter Treeview?
- How to clear a Tkinter ListBox?
- How to fit Tkinter listbox to contents?
- How to keep selections highlighted in a Tkinter Listbox?
- Default to and select the first item in Tkinter Listbox
- How to fully change the color of a Tkinter Listbox?
- How to directly modify a specific item in a TKinter listbox?
- How do you remove multiple items from a list in Python?
- How to display a Listbox with columns using Tkinter?
- How to select at the same time from two Tkinter Listbox?
- Attach scrollbar to listbox as opposed to window in Tkinter
- How to get the index of selected option in Tkinter Combobox?
- How can I change the text of the Tkinter Listbox item?
- Resize the Tkinter Listbox widget when the window resizes