pythongtkpygtkgladegtkbuilder

pygtk gtk.Builder.connect_signals onto multiple objects?


I am updating some code from using libglade to GtkBuilder, which is supposed to be the way of the future.

With gtk.glade, you could call glade_xml.signal_autoconnect(...) repeatedly to connect signals onto objects of different classes corresponding to different windows in the program. However Builder.connect_signals seems to work only once, and (therefore) to give warnings about any handlers that aren't defined in the first class that's passed in.

I realize I can connect them manually but this seems a bit laborious. (Or for that matter I could use some getattr hackery to let it connect them through a proxy to all the objects...)

Is it a bug there's no function to hook up handlers across multiple objects? Or am I missing something?

Someone else has a similar problem http://www.gtkforums.com/about1514.html which I assume means this can't be done.


Solution

  • Here's what I currently have. Feel free to use it, or to suggest something better:

    class HandlerFinder(object):
        """Searches for handler implementations across multiple objects.
        """
        # See <http://stackoverflow.com/questions/4637792> for why this is
        # necessary.
    
        def __init__(self, backing_objects):
            self.backing_objects = backing_objects
    
        def __getattr__(self, name):
            for o in self.backing_objects:
                if hasattr(o, name):
                    return getattr(o, name)
            else:
                raise AttributeError("%r not found on any of %r"
                    % (name, self.backing_objects))