Pranav Talluri
Pranav Talluri

Reputation: 11

How to remove multiple selected items in listbox in tkinter Python?

I want to be able to delete all selected items when I use selectmode=MULTIPLE. I have tried to delete but it only deletes the item that was selected last. Is there any way to delete all of the items.

Thanks

from tkinter import *

def abc():
    listbox.delete(ACTIVE)

def xyz():
    z=listbox.get(0, END)
    print (z)

master = Tk()
scrollbar = Scrollbar(master,orient=VERTICAL)
listbox = Listbox(master, yscrollcommand=scrollbar.set, selectmode=MULTIPLE)
scrollbar.config(command=listbox.yview)
b = Button(master, text="delete", command=abc)
b.pack(side=RIGHT)
b2 = Button(master, text="save", command=xyz)
b2.pack(side=RIGHT)
scrollbar.pack(side= RIGHT, fill=Y)
listbox.pack(side=LEFT)

for item in ["one", "two", "three", "four", "five"]:
    listbox.insert(END, item)

mainloop()

Upvotes: 1

Views: 8157

Answers (4)

Ankush Bhagat
Ankush Bhagat

Reputation: 36

You can remove selected items of lsbox using python build-in revered method:

def delete_task():
    selections = lsbox.curselection()
    for selection in reversed(selections):
        lsbox.delete(selection)

Upvotes: 0

Raj
Raj

Reputation: 41

Only half of selected items are deleted.

I think the index is recreated after each delete so items reassigned to a deleted index will not be deleted.

item index: 0,1,2,3 after deleting 0 becomes 0,1,2 then it thinks that 0 is already deleted so deletes 1. After deleting 1 the index is 0,1 - which are both already deleted so nothing more to delete.

ANSWER: The trick is to delete the selected items in reverse order so that items earlier in the list are not reindexed by delete:

def call_delete():
    selection = listBox.curselection()
    for i in reversed(selection):
        listBox.delete(i)

Upvotes: 4

Wesley Bowman
Wesley Bowman

Reputation: 1396

EDIT The better answer is not this one, but leaving this here for the documentation and the url. Check out https://stackoverflow.com/a/44818820/1141389

Take a look here for more info on Listbox wiget

You can clear the whole listbox with

listbox.delete(0, END)

If you only want the selected items deleted, I think you could do something like the following:

def abc():
    items = map(int, listbox.curselection())
    for item in items:
        listbox.delete(item)

but note that I cannot test this currently. Try the above and checkout the website, that should get you on the right track.

Upvotes: -2

j_4321
j_4321

Reputation: 16169

To get all selected items instead of only the last one, you can use listbox.curselection() and then delete them one by one, starting from the last one so that the indexes of the other ones are not modified by the deletion.

def abc():
    sel = listbox.curselection()
    for index in sel[::-1]:
        listbox.delete(index)

By the way, I advise you to give meaningful names to your functions (like "delete" instead of "abc").

Upvotes: 11

Related Questions