Wednesday, 23 January 2008

HOWTO: listbox in Python Tkinter

alist=[' x ',' xin ','zhengxin',' shan ',' shanshan ','shanshan cheng ']
##aset=set(alist)
##b=set()
##
##for aitem in aset:
## if aitem.find('xi')!=-1:
## print aitem,':item will be removed'
## b.add(aitem)
##
##c=aset-b
##print aset
##print c

from Tkinter import *
class MyDialog:
def __init__(self,master):
MyDialog.removed=list()
Label(text="one").pack()
self.s = Frame()
self.s.pack()

self.listbox = Listbox(self.s,selectmode=EXTENDED)
self.listbox.pack()

for item in alist:
self.listbox.insert(END, item)

Label(text="two").pack()

self.b = Button(master, text="Delete",command = self.toremove)
## self.b = Button(master, text="Delete",command = lambda lb=lb: lb.delete(ANCHOR))
self.b.pack()

def toremove(self):
# Select what to remove and save them to the Class Variable MyDialog.removed
self.items = self.listbox.curselection()
for i in self.items:
MyDialog.removed.append(self.listbox.get(i))
print self.listbox.get(i)

# To sort the sequence,then reverse, then delete.
self.iitems=list()
for i in range(len(self.items)):
self.iitems.append(int(self.items[i]))
self.iitems.sort()
self.iitems.reverse()
for j in self.iitems:
self.listbox.delete(j)
## def toremove(self):
## self.listbox.delete(ANCHOR)


if __name__=='__main__':
root = Tk()
d = MyDialog(root)
root.mainloop()

No comments:

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter