attaching Tkinter Listbox to python list object

S

Steve Potter

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...read/thread/3a6fe7534c812f55/43f201ab53cfdbf7
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
 
J

James Stroud

Steve said:
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...read/thread/3a6fe7534c812f55/43f201ab53cfdbf7
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
 
J

jim-on-linux

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.lang.python
/browse_thread/thread/3a6fe7534c812f55/43f201ab5
3cfdbf7 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

Look into the StringVar(),
class for Tkinter.

var = stringVar()

sorry I can't help more, but I think this is what
you are looking for.

jim-on-linux

http:\\www.inqvista.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top