[pygtk] Ok. A new try...

John Ehresman jpe at wingware.com
Thu Mar 23 11:17:31 WST 2006


Gonzalo Aguilar Delgado wrote:
> How do I find a widget (say a GtkEntry) inside it with the name
> "txtbox"?

You need to go through all the children and find the one with the name. 
   Try the following:

def all_descendants(w, flags=None):
   """ Generator to traverse all descendants of a widget in depth-first 
order.
   Assumes that the widget hierarchy doesn't change during the 
traversal.  Use
   flags to only return widgets where widget & flags is true.  If a 
widget is
   filtered out, all of its descendants are also filtered out. """

   if not isinstance(w, gtk.Container):
     return

   for child in w.get_children():
     if flags is None or child.flags() & flags:
       yield child

       for g_kid in all_descendants(child):
         yield g_kid

def find_named(parent, name):
   for w in all_descendants(parent):
     if w.get_name() = name:
       return w
   return None

entry = find_named(frame, 'txtbox')

You probably do want to make sure that only one widget with the name 
'txtbox' is a descendant of frame.  gtk does not enforce this for you.

John



More information about the pygtk mailing list