我在使用tkinter删除列表框的未选项时遇到了一些麻烦,它有时会工作....但主要是失败,问题是当你选择多个项目并点击" Ocultar没有选择"按钮,您必须删除所有未选中的项目。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#spyder
from Tkinter import *
import tkFileDialog
import os, sys
def curdir():
cdir = tkFileDialog.askdirectory(parent=finestra, initialdir="/home")
v.set(cdir)
def omplir():
path=e1.get()
dirs = os.listdir( path )
for file in dirs:
listbox.insert(END,file)
def reomplir():
path=e1.get()
listbox.delete(0,END)
dirs = os.listdir( path )
for file in dirs:
listbox.insert(END,file)
def ons():
ar=''
c=0
for i in listbox.curselection():
ar=listbox.curselection()
c=len(ar)
listbox.delete(ar[c-1]+1,END)
c=c-1
while(c!=0):
listbox.delete(ar[c-1]+1)
c=c-1
if ar[c]!=0:
listbox.delete(0)
def oss():
ar=''
for int in listbox.curselection():
ar=listbox.curselection()
i=len(ar)-1
while(i>=0):
listbox.delete(ar[i])
i=i-1
def tots():
for int in range(listbox.size()):
ex=False
for i in range(lbox.size()):
if listbox.get(int) == lbox.get(i):
ex=True
break;
if ex is False:
lbox.insert(END,listbox.get(int))
def afg():
for int in listbox.curselection():
ex=False
for i in range(listbox.size()):
if listbox.get(int) == lbox.get(i):
ex=True
break;
if ex is False:
lbox.insert(END,listbox.get(int))
def dlt():
ar=''
for int in lbox.curselection():
ar=lbox.curselection()
i=len(ar)-1
while(i>=0):
lbox.delete(ar[i])
i=i-1
def cap():
lbox.delete(0,END)
#MAIN
finestra = Tk()
finestra.title("Creacio de fitxer comprimit")
f=Frame(finestra)
f.pack(side=TOP)
b=Button(f,text='Escollir directori treball',command=curdir)
b.pack(side=LEFT,anchor=W)
v=StringVar()
v.set("/home")
e1=Entry(f,width=35,textvariable=v)
e1.pack(side=LEFT)
l1=Label(f,text="Fitxers a incorporar al fitxer tar:")
l1.pack(side=TOP,anchor=N,padx=120)
f1=Frame(finestra)
f1.pack(side=TOP,anchor=NW)
l2=Label(f1,text="Llista:")
l2.pack(side=LEFT)
br=Button(f1,text='Reomplir',command=reomplir)
br.pack(side=LEFT)
bo=Button(f1,text='Ocultar no seleccionats',command=ons)
bo.pack(side=LEFT)
bos=Button(f1,text='Ocultar seleccionats',command=oss)
bos.pack(side=LEFT)
Label(f1,text="\t\tCompresió").pack(side=LEFT)
rb1=Radiobutton(f1,text="cap").pack(side=LEFT)
rb2=Radiobutton(f1,text="gzip",value="gzip").pack(side=LEFT)
rb3=Radiobutton(f1,text="bzip2",value="bzip2").pack(side=LEFT)
rb4=Radiobutton(f1,text="xz",value="xz").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,selectmode=EXTENDED)
listbox.pack(side=TOP,anchor=W,fill="both",expand=True)
scrollbar.config(command=listbox.yview)
omplir()
f3=Frame(finestra)
f3.pack(side=LEFT)
Label(f3,text="Tots:").pack(side=TOP,anchor=W)
tots=Button(f3,text=">>>",command=tots).pack(side=TOP)
Label(f3,text="Als seleccionats:").pack(side=TOP)
af=Button(f3,text="-->",command=afg).pack(side=TOP)
qt=Button(f3,text="<--",command=dlt).pack(side=TOP)
Label(f3,text="Tots:").pack(side=TOP,anchor=W)
cap=Button(f3,text="<<<",command=cap).pack(side=TOP)
f4=Frame(finestra)
f4.pack(side=TOP)
sbar = Scrollbar(f4)
sbar.pack(side=RIGHT, fill=Y)
lbox = Listbox(f4, bd=0, yscrollcommand=sbar.set,width=35,selectmode=EXTENDED)
lbox.pack(side=LEFT,padx=5)
sbar.config(command=lbox.yview)
f6=Frame(finestra)
f6.pack(side=TOP,anchor=W,padx=20)
Button(f6,text="Crea").pack(side=LEFT)
Label(f6,text="fitxer tar:").pack(side=LEFT)
f5=Frame(f2)
f5.pack(side=BOTTOM,anchor=W)
Button(f5,text="Sortir",command=finestra.destroy).pack(side=BOTTOM,anchor=S)
mainloop( )
答案 0 :(得分:0)
解决方案是添加用最终空格分隔的所有单词,然后重建列表框内容。
def ons():
cs=StringVar
cs=""
a=''
for i in range(listbox.size()):
if listbox.select_includes(i):
cs +=listbox.get(i)+' ' #adding words ending with whitespace
listbox.delete(0,END) #deleting all the listbox content
for item in cs:
a=a+item
if item==' ': #full string passed
listbox.insert(END,a) #insert selected item into listbox
a=''#delete the string buffer
答案 1 :(得分:0)
我使用此功能删除所选项目:
def _del():
n = 0
for i in lb.curselection():
lb.delete(i - n)
n += 1
Button(f3, text='Del', relief=GROOVE, command=_del).pack(fill=X)
也可以使用此方法删除未选中的项目,
def _delUsl():
file_lsObj.set([lb.get(i) for i in lb.curselection()])