[pygtk] Tutorial Question...

dave dave at immunitysec.com
Sun Aug 8 19:59:09 WST 2004

>> 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

If I'm not mistaken, row.iterchildren() actually returns a pointer to 
all the children in the row, not the children in the tree (which is 
really what I think we wanted here).

Don't laugh too hard, but this is what I came up with.

def findobj(model,searchobj,current):
    #print "row[0]=%s searchobj=%s"%(row,searchobj)
    if row==searchobj:
        #print "Found! - returning %s"%(myiter)
        return myiter
        if model.iter_has_child(myiter):
            while childiter!=None:
                if myiter!=None:
                    return myiter
    return myiter
#where parentobj is what I am searching for

It seems to work. Now I'm moving on to thinking about adding nodes to 
the tree and having it update intelligently, etc. :>

(note: I'm not averse to putting any of this stuff in the FAQ, if you 
guys think anything I'm posting is worth putting into the FAQ. I guess 
that goes without saying around here though. :>)


More information about the pygtk mailing list