[pygtk] Tutorial Question...

John Finlay finlay at moeraki.com
Sun Aug 8 19:44:58 WST 2004


dave wrote:

> http://www.pygtk.org/pygtk2tutorial/sec-TreeModelInterface.html
>
> A PyGTK TreeModelRow object supports the Python sequence and iterator 
> protocols. You can get an iterator to iterate over the column values 
> in the row or use the for statement or list comprehension as well. A 
> TreeModelRow uses the column number as the index to extract a value. 
> For example:
>
>  ...
>  liststore = gtk.ListStore(str, int)
>  liststore.append(['Random string', 514])
>  ...
>  row = liststore[0]
>  value1 = row[1]
>  value0 = liststore['0'][0]
>  ...
>     
>
> Are we supposed to be talking about TreeStore's here instead of 
> liststores?

We are talking about liststores in the above example though I can see 
how you might be confused since the next example uses a treestore.

>
> I'm currently doing a search on a TreeStore:
> for row in treestore:
>  if row[0]==searchedthing:
>     return row
>
> Hopefully that's going to work, although it'd be nice to have a clear 
> example in the tutorial for non-gui-geniuses like myself.
>
Depends what you think is going to happen. In this case you will iterate 
over the toplevels rows of the treestore and get the value that's in the 
first column of each toplevel row for comparison. It will not iterate 
over the child rows of the toplevel rows. To iterate over all rows in a 
treestore you'll have to get the children of each row and iterate over 
them. Something like the following should work:

def match_row(rows, value):
    if not rows: return None
        for row in rows:
            if row[0] == value: return row
            result = match_row(row.iterchildren(), value)
            if result: return result
    return None

John



More information about the pygtk mailing list