Steve Potter wrote:
I am trying to find some method of attaching a Listbox object to a
list object so as the contents of the list are changed the contents of
the Listbox will be updated to match. I have found a few references
to something like this in this old post
http://groups.google.com/group/comp....f201ab53cfdbf7
as well as here http://effbot.org/zone/wck-4.htm .
It just seems that there should be some way of achieving this.
The only this I can think of is create a subclass of list that deletes
and then refills the Listbox every time that the list changes, but
this seems very in efficient.
Any ideas?
Steve
If you want the listbox to have the interface of a list, you will
probably not want to do it by inheritance because you are going to have
to re-write a lot of methods anyway, calling super, etc.
Probably better is composition in this case, even though inheritance
feels like the right way if you haven't tried it yet and discovered its
problems.
Inherit from object and fill the class with objects designed for the
job. In fact, make sure you want to abstract your data structure to list
before you even use that--then create a controller that retrieves
appropriate info from the data structure (model) and updates the listbox
(view) accordingly. Use callbacks from the listbox to notify the
controller if the events in the listbox require modification of the
list. Have the controller modify the list and then use the modified list
to update the listbox.
This is more-or-less MVC. MVC can be as simple as listbox, list, object,
or more complicated. But you will be happy you began with an MVC design
at some point in the future. You'll really be happy that you didn't
inherit from list for this!
I've looked into the listbox issue and I have found that you will have
to delete and refill the listbox every time the list is changed. I
wouldn't worry about speed, though, your computer will keep up with the
user, no problem. If you look into other GUI frameworks, you'll find
that they all seem to delete and refill at some level. Its just with
Tkinter, you have to do this explicitly, which never feels quite right.
James