如何内省地将处理程序连接到信号? [英] How to introspectively connect handlers to signals?

查看:77
本文介绍了如何内省地将处理程序连接到信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gtk.Builder 能够识别GUI的所有信号(在XML文件中描述)可以发出

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):
        ...

在我的应用程序中,我还有其他信号是由非GUI对象生成的(这是我的模型(如MVC模式),当其内部状态更改时会发出信号),但是需要由GUI处理.

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.

我正在尝试找到一种方法,该方法可以自动连接到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):
        ...

是否有标准的[py] GTK方法来实现这一目标,或者我必须编写自己的gtk.Builder子类?

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

提前感谢您的时间!

推荐答案

GtkBuilder从XML获取要连接的名称,它不搜索以"on"开头的方法.这意味着您的模型需要用XML表示,无法将您在代码中实例化的小部件传递给GtkBuilder. Glade有有关自定义小部件的文档.

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.

这篇关于如何内省地将处理程序连接到信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆