[pygtk] Re: Trouble getting a ListStore with dynamic column number.

Jeffrey Barish jeff_barish at earthlink.net
Fri Feb 1 10:20:44 WST 2008


Neil Dugan wrote:

> Stephen George wrote:
>> Neil Dugan wrote:
>> 
>>> Hi,
>>>
>>> I am trying to create a dialog with a gtk.TreeView() with a
>>> gtk.ListStore() model.  I don't know until run time how many columns
>>> will be needed.  I have managed to create the TreeView but I haven't
>>> worked out how to populate it.
>>>
>>> Any help appreciated.
>>>
>>> Regards Neil.
>> 
>> Hi Neil,
>> 
>> I think your problem steams from the line
>> 
>> dialog._model = gtk.ListStore(object)
>> 
>> Which I think should be more like
>> 
>> dialog._model = gtk.ListStore(str,str,str)
>> 
>> However this does not give you the dynamic nature you desire.
>> 
>> I don't know the limits of column expansion/contraction you require for
>> your problem, .. but I see in your code you want to limit it to 20
>> (are they always strings?, or all columns the same type?)
>> 
>> I would create a listStore with 20 str as arguments.
>> 
>> I would then create the treeview with as many columns needed for the
>> problem at hand
>> 
>> Attached you will see a modified prog that works for me, I limited the
>> columns to 7, each time you press the dialog button, a list is
>> dynamically generated with a random number of columns.
>> Assumptions:
>> the list is always strings.
>> length of list correlates to number of colomns to view.
>> 
>> Don't know if this has been helpful?
>> 
>> Steve
>> 
>> 
> 
> Hi Steve,
> 
> Thanks, that does do what I want.  :)
> 
> After posting I found out that, that line was the trouble, but I
> couldn't find a decent way of handling the problem.  I didn't realise
> you could specify more types when creating the gtk.ListStore than you
> had intentions of using.
> 
> I think it would be handy to be able to use a list or truple to
> specify the types as well.
> 
> Regards Neil.

If I remember correctly, the solution that I used originally was something
like

dialog._model = gtk.ListStore(*([str] * ncols))

which creates exactly ncols columns of type str.

The solution that I use now is to put a Python class in column 0 and then
use set_cell_data_func to access the appropriate attribute of the class (or
other Python object) corresponding to the indicated column of the TreeView. 
This is the better solution because the GTK code is oblivious to the number
of columns as that information has been moved into Python.

There's a FAQ on how to use this feature of GTK if you are not familiar with
it:

http://faq.pygtk.org/index.py?req=show&file=faq13.029.htp
-- 
Jeffrey Barish



More information about the pygtk mailing list