[pygtk] Liststore model
Richard Taylor
rjt-pygtk at thegrindstone.me.uk
Sun Jan 14 16:14:30 WST 2007
Another option is to wrap the return in a class instance an put that in the
ListStore.
store = gtk.ListStore(object)
Then write a gtk.TreeView that takes the cur.description and build the correct
columns. You can use TreeViewColumn.set_cell_data_func to set an method that
gets called to fill in the column.
Here is a small part of the code from Gramps that does this, although it does
not calculate anything dynamically you should able to see how to do it:
class PersonTreeView(gtk.TreeView):
def __init__(self,db,apply_filter=None):
gtk.TreeView.__init__(self)
self._db = db
# Add the Name column
cols = (\
(_("Name"),300,self._family_name),\
(_("ID"),100,self._object_id),\
(_("Gender"),100,self._gender),\
(_("Birth Date"),200,self._birth_date),\
(_("Birth Place"),200,self._birth_place),\
(_("Death Date"),200,self._death_date),\
(_("Death Place"),200,self._death_place),\
(_("Spouse"),200,self._spouce),\
(_("Last Change"),200,self._last_change),\
(_("Cause of Death"),300,self._death_cause))
for col in cols:
self.append_column(
self._new_column(
col[0],col[1],col[2]))
self.set_enable_search(False)
self.set_fixed_height_mode(True)
def _new_column(self,name,size,func):
col = gtk.TreeViewColumn(name)
col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
col.set_fixed_width(size)
cell = gtk.CellRendererText()
col.pack_start(cell,True)
col.set_cell_data_func(cell,func)
return col
If you replaces cols with something generated from the cur.description
information you would be half way there.
Richard
On Saturday 13 January 2007 02:30, Seth Mahoney 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.
>
> OR (and I'm not sure this will work for you either), just have one
> column, and create its data on the fly by joining strings. So:
>
> store = gtk.ListStore(str)
> store.append([data1 + " " + data2 + " " + data 3 ...)
>
> On Fri, 2007-01-12 at 09:52 -0500, Chris Lambacher wrote:
> > On Fri, Jan 12, 2007 at 03:32:05PM +0100, Volker Helm wrote:
> > > Hi Jarek,
> > >
> > > > > 1. A select is send to a database:
> > > > > cur.execute("select position,article,name,amount,value from
> > > >
> > > > table_bill_pos")
> > > >
> > > > > pos = cur.fetchall()
> > > > >
> > > > > 2. depending on the result, I generate the model:
> > > > > listmodel = gtk.ListStore(int,str,str,str,str)
> >
> > columns = [int, str, str, int]
> > listmodel = gtk.ListStore(*columns)
> > columns.append(str)
> > listmodel = gtk.ListStore(*columns)
> >
> >
> > see http://docs.python.org/tut/node6.html#SECTION006740000000000000000
> >
> > > > > 3. create the TreeView:
> > > > > tv = gtk.TreeView(listmodel)
> > > > >
> > > > > My problem is the second part, since I've got many queries against
> > > > > the
> > > >
> > > > database and changing the queries quite often.
> > > >
> > > > > So, I want to generate the _listmodel_ dynamically. Next time,
> > > > > maybe the
> > > >
> > > > query could be something like (int,str,str,str,str,str,int), because
> > > > I added two columns.
> > > >
> > > > Cursor objects have description. It's a tuple, where second element
> > > > is typecode. You can build your ListStore using that information.
> > >
> > > that sound great, but I'm blocked. I using cur.description to get the
> > > information of each column (type and header). But I don't know a way to
> > > generate the gtk.ListStore() dynamically.
> >
> > See my comments above
> >
> > > Can you give me some code that shows the way of doing it. I tried to
> > > generate a basic gtk.ListStore(int) model, then appending further
> > > columns, but I could get it work.
> > >
> > > If I solve this problem, I would make a little tutorial about this,
> > > maybe someone has similar problems.
> > >
> > > Thanks you,
> >
> > Your welcome,
> > Chris
> > _______________________________________________
> > pygtk mailing list pygtk at daa.com.au
> > http://www.daa.com.au/mailman/listinfo/pygtk
> > Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
>
> _______________________________________________
> pygtk mailing list pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
More information about the pygtk
mailing list