Search code examples
pythongtkpygtk

pygtk get widget by name from parent node


I have a gtk widget and I want to find out if within its descendants there is another widget. If there is one, I want to return it otherwise return None. This is a simple recursive problem but I cannot seem to get the right method to do it.

In the glade xml file, I have:

  <object class="GtkDialog" id="monkey">
  [...]
       <object class="GtkTreeView" id="ook">

and a call to find(my_monkey_object, 'ook') should return the GtkTreeView object. find() should be something akin to

def find (node, id):
    if node.XXX() == id: return node
    for child in node.get_children():
        ret = find(child, id)
        if ret: return ret
    return None

I am not sure which XXX() method I need to use. get_name() looked hopeful but returns the class name of the object and not its "id". The version I use is pygtk-2.24.

See this Python GTK+ widget name question for the same problem.

Note that this bug kind of explains the issue: I want the builder ID as from a GTK widget tree. Sadly, this seems impossible to get...


Solution

  • According to the gtk C-api documentation, you can get the glade "id" name like this:

    name = gtk_buildable_get_name (GTK_BUILDABLE (widget))
    

    For pygtk, this is the same as

    name = gtk.Buildable.get_name(widget)