tags:

views:

154

answers:

2

Building a GTK+ widget dynamically from code allows for easy access to the child widgets directly.

Now, how do I access to the child widgets when building a GTK+ Dialog (as example) from a .glade file?

class ConfigDialog(object):
    def __init__(self, glade_file, testing=False):
        self.testing=testing
        builder = gtk.Builder()
        builder.add_from_file(glade_file)
        self.dialog = builder.get_object("config_dialog")
        self.dialog._testing=testing
        self.dialog._builder=builder

I've tinkering a bit with .get_internal_child without success.

Q: let's say I want to access the widget named "name_entry", how would I go about it?

+3  A: 

Already you are making the call

self.dialog = builder.get_object("config_dialog")

You should also be able to do

self.nameEntry = builder.get_object("name_entry")

This is at least how python-glade works and I assume GtkBuilder is the same.

Ed
Marvelous! That did the trick! I can't "up vote" you at the moment... I haven't got any "votes" left for today... quota reached. I'll do that tomorrow.
jldupont
Why did you put your answer as a Community Wiki? Can't earn reputation this way...
jldupont
@juldupont: Probably because Ed felt like it.
Georg
1. Didn't realize no reputation is added and I'm not sure how much I should care about reputation.2. I would rather people improve my answer if they feel it lacking rather than post a one-off of it and splitting the votes.
Ed
+2  A: 

In addition, if you want to search for a named widget and the Builder instance isn't available, you could try using the following utility function:

def get_child_by_name(parent, name):
    """
    Iterate through a gtk container, `parent`, 
    and return the widget with the name `name`.
    """
    def iterate_children(widget, name):
        if widget.get_name() == name:
            return widget
        try:
            for w in widget.get_children():
                result = iterate_children(w, name)
                if result is not None:
                    return result
                else:
                    continue
        except AttributeError:
            pass
    return iterate_children(parent, name)
Donald Harvey