How to introspectively connect handlers to signals?

天涯浪子 提交于 2020-01-04 04:39:08

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/6487677/how-to-introspectively-connect-handlers-to-signals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!