简体   繁体   中英

How to introspectively connect handlers to signals?

gtk.Builder is capable to identify all signals that a GUI (described in a XML file) can emit and with the method connect_signals() automagically matches signals and handlers. Example:

class Gui(gobject.GObject):

    def __init__(self):
        self.gui_file = "../data/gui.xml"
        builder = gtk.Builder()
        builder.add_from_file(self.gui_file)
        builder.connect_signals(self)

    def on_whatever_gui_event(self, widget, data=None):
        ...

In my application I have other signals that are generated by non-GUI objects (it's my Model [as in the MVC pattern] that emits a signal when its internal state is changed) but that need to be handled by the GUI.

I am trying to find a method that would allow me to automagically connect to the Gui instance also my custom signals. In other word, I'm trying not to have to manually connect each signal to it's handler. Ideally the final code should look like:

class Gui(gobject.GObject):

    def __init__(self, model_instance):
        self.gui_file = "../data/gui.xml"
        builder = gtk.Builder()
        builder.add_from_file(self.gui_file)
        builder.add_signals_from_my_object(model_instance)
        builder.connect_signals(self)

    def on_whatever_gui_event(self, widget, data=None):
        ...

    def on_whatever_model_event(self, widget, data=None):
        ...

Is there a standard [py]GTK way to achieve this or do I have to write my own child class of gtk.Builder?

Thank you in advance for your time!

GtkBuilder takes the names to connect to from the XML, it does not search for methods starting with 'on'. This means your model needs to be represented in the XML, there is no way to pass GtkBuilder a widget you instantiated in code. Glade has documentation on custom widgets .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM