[pygtk] Liststore model

Florian Diesch diesch at spamfence.net
Sun Jan 14 08:40:29 WST 2007


Seth Mahoney <smahoney at pdx.edu> wrote:

> If this doesn't work, then I've got another idea (though I don't know if
> it will work for you, either).  You could try generating the liststore
> with a str for every possible column, and then only show the columns you
> need.  So, if there were five different data items:
>
> store = gtk.ListStore(str, str, str, str, str)
>
> and then use gtk.TreeView.append_column() and
> gtk.TreeView.remove_column() to hide and show columns as necessary.

Another way is to use a ListStore with a column containing complex data
like doicts, lists or objects and then use a cell_data_func to access
its attributes. Like this (extracted from some of my code, maybeneeds
some fixing, but the idea should be clear):

class Foo(object):
 def __init__(self, bar):
    self.bar=bar

self.model=gtk.ListStore(gobject.TYPE_PYOBJECT)
self.model.append([Foo(i) for i in range(0,5)])
self.widget.set_model(self.model)

cell=gtk.CellRendererText()
col=gtk.TreeViewColumn(self.title)
col.pack_start(cell, True)
def callback(column, cell, model, iter):
   cell.set_property('text', 
                     getattr(model.get_value(iter, 0), 'bar'))
col.set_cell_data_func(cell, callback)
self.widget.append_column(col)




   Florian
-- 
<http://www.florian-diesch.de/>


More information about the pygtk mailing list