[Tutor] How do I update a listbox?

Suzanne Little s349929@student.uq.edu.au
Wed, 12 Jan 2000 12:54:52 +1000 (GMT+1000)


Hello, 

I'm having some problems updating/refreshing elements in windows. I'm
using tkinter to build a main gui which contains a listbox filled with
elements of a list. When you click a button in this gui another window
appears which contains buttons to perform actions upon the listbox such 
as add, delete. How do I get the listbox to reflect the changes? I have a
method, updateList which removes all elements and inserts the new list but
that doesn't seem to work. Code for a greatly simplified trial version is
attached at the end of this email. Any direct comments on the code would
be greatly appreciated but if anyone could also recommend some references
or examples on updating that would be excellent since I have a few more
problems to solve and I would like to have a good understanding of it.

Thanks, 

Suzanne

#####list.py#####

from Tkinter import *
from listComp import *

names = ['hello1', 'hello2', 'hello3', 'hello4', 'hello5']

class TrialListbox(Frame):
    def __init__(self, parent=None):
	Frame.__init__(self, parent)
	self.pack()
	self.createWidgets()
	self.master.title('A Listbox Trial')
	self.master.iconname('tkpython')

    def createWidgets(self):
	self.makeList()
	self.makeButtons()

    def makeList(self):
	frame = Frame(self)
	frame.pack()
	scrollbar = Scrollbar(frame, orient=VERTICAL)
	self.listbox = Listbox(frame, yscrollcommand=scrollbar.set)
	scrollbar.config(command=self.listbox.yview)
	scrollbar.pack(side=RIGHT, fill=Y)
	self.listbox.pack(side=LEFT, fill=BOTH, expand=1)
	for i in names:
	    self.listbox.insert(END, i)

    def updateList(self):
	self.listbox.delete(0, END)
	for i in names:
	    self.listbox.insert(END, i)

    def makeButtons(self):
	bframe = Frame(self)
	bframe.pack(side=BOTTOM)
	Button(bframe, text='Call Print', command=self.callPrint).pack(side=BOTTOM)
	Button(bframe, text='Quit', command=self.getOut).pack(side=BOTTOM)

    def callPrint(self):
	w1 = Toplevel()
	w1.window = ListComp(w1, self)
	w1.window.pack()

    def getOut(self):
	Frame.quit(self)

if __name__ == '__main__': TrialListbox().mainloop()


#####listComp.py#####

from Tkinter import *
from list import *

class ListComp(Frame):

    def __init__(self, parent=None, listBox=None):
	Frame.__init__(self, parent)
	self.trialListbox = listBox
	self.w1 = parent
	self.pack()
	self.createWidgets()

    def createWidgets(self):
	bframe = Frame(self)
	bframe.pack()
	Button(bframe, text='Print', command=self.Iprint).pack(side=LEFT)
	Button(bframe, text='Quit', command=self.Iquit).pack(side=LEFT)
	Button(bframe, text='Add', command=self.Iadd).pack(side=LEFT)
	Button(bframe, text='Delete', command=self.Idel).pack(side=LEFT)

    def Iprint(self):
	if self.trialListbox != None:
	    item = self.trialListbox.listbox.curselection()
	    member = self.trialListbox.listbox.get(item)
	    print 'Term: ' + member

    def Iquit(self):
	self.w1.destroy()

    def Iadd(self):
	names.append('hello')
	self.trialListbox.updateList()

    def Idel(self):
	item = self.trialListbox.listbox.curselection()
	member = self.trialListbox.listbox.get(item)
	names.remove(member)
	self.trialListbox.updateList()

-----------------------------------------------------------------------
"Contrariwise," continued Tweedledee, "If it was so, it might be; and if
it were so, it would be; but as it isn't, it ain't.  That's logic"
                             -Lewis Carroll
------------------------------------------------------------------------