[[!tag programming gnome]]

I like the following hack:

def find_and_bind_widgets(self, builder):
    for o in builder.get_objects():
        if isinstance(o, gtk.Widget):
            name = o.get_property('name')
            for attr in dir(self):
                prefix = 'on_%s_' % name
                if attr.startswith(prefix):
                    signal_name = attr[len(prefix):]
                    method = getattr(self, attr)
                    o.connect(signal_name, method)

Explanation: In a PyGTK program I am working on, the UI has been laid out using Glade and GtkBuilder. The above method goes through all widgets in the .glade file, and looks for methods following a specific naming convention: on_widgetname_signalname. It then connects the method to the signal and widget.

For example, I have a widget called window, and a method called on_window_delete_event, which gets called when the window manager tells the app the close the window.

This lets me just name widgets in Glade and the Python file, without having to do any extra work. I do not even need to set the name of the signal handler in Glade. If the method is named right, it gets connected.