PyGTK连接信号

2024-06-12 05:26:45 发布

您现在位置:Python中文网/ 问答频道 /正文

好的,我这里有一个小测试程序:

这是我的文件,我加载通过gtk建造者在

<object class="GtkWindow" id="mainWindow">
    <property name="default_width">500</property>
    <property name="default_height">250</property>
    <signal name="delete_event" handler="endProgram" />
</object>

然后我用这个:

^{pr2}$

但是当我运行时,它会抱怨我缺少主窗口对象的处理程序。 我试过了rofl.connect U信号(获胜);以及


Tags: 文件nameiddefaultgtksignalobjectproperty
1条回答
网友
1楼 · 发布于 2024-06-12 05:26:45

根据the docsconnect_signals将映射或实例作为参数,并且

uses Python's introspective features to look at the keys (if object is a mapping) or attributes (if object is an instance) and tries to match them with the signal handler names given in the interface description. The callbacks referenced by each matched key or attribute are connected to their matching signals.

因此,例如,当您传递"mainwindow",它是str的一个实例时,这些属性是例如upperlowerisalpha之类的方法名,与您可能远程感兴趣的任何内容无关。为什么还要让win的属性来处理信号呢?你希望connect_signals做什么?在

更典型的示例用法可以在SO questionthis tutorial中找到,其中包括以下Python示例:

class TutorialTextEditor:

    def on_window_destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self):

        builder = gtk.Builder()
        builder.add_from_file("tutorial.xml") 

        self.window = builder.get_object("window")
        builder.connect_signals(self)     

如您所见,这里connect_signals是以典型的方式使用的,即传递一个带有on_window_destroy方法的对象(self),该方法(通过自省)将用作窗口破坏时引发的信号的处理程序。在

相关问题 更多 >