Ulrok
Ulrok

Reputation: 89

tkinter delete all but not selected item

i am trying to delete all the items of a listbox but not the one i selected,since listbox.delete(index,last) delete index and last included so the current selection is allways deleted,what shall i do?,i let you my code so you can see what i've been trying

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#spyder
from Tkinter import *
import tkFileDialog
import os, sys
def ons():
    cs=listbox.curselection()
    listbox.delete(0,cs)
    listbox.delete(cs,END)

finestra = Tk()
finestra.title("Creacio de fitxer comprimit")
f=Frame(finestra)
f.pack(side=TOP)

bo=Button(f1,text='Ocultar no seleccionats',command=ons)
bo.pack(side=LEFT)

f2=Frame(finestra)
f2.pack(side=LEFT,anchor=W,pady=0)


scrollbar = Scrollbar(f2)
scrollbar.pack(side=RIGHT,fill="y",expand=False)

listbox = Listbox(f2, bd=0, yscrollcommand=scrollbar.set,width=55)

listbox.pack(side=TOP,anchor=W,fill="both",expand=True)

scrollbar.config(command=listbox.yview)

Upvotes: 2

Views: 6154

Answers (1)

Eric Levieil
Eric Levieil

Reputation: 3574

def ons():
    cs=listbox.curselection()
    listbox.delete(0,cs[0] -1)
    listbox.delete(1,END)

should work.

Upvotes: 6

Related Questions