PyGTK connect_signals

发布于 2024-09-12 11:28:45 字数 655 浏览 3 评论 0原文

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

这是在我通过 gtk.Builder 加载的文件中,

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

然后我使用这个:

def endProgram ():
    print "end";

rofl = gtk.Builder();

rofl.add_from_file("mainwindow.ui");
win = rofl.get_object("mainWindow");
rofl.connect_signals("mainWindow");
win.show_all();
gtk.main();

然而,当我去运行时,它抱怨我缺少 mainWindow 对象的处理程序。 我尝试过 rofl.connect_signals(win);还有

Ok so I have a little test program here:

This is in my file that I load through gtk.Builder

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

I then use this:

def endProgram ():
    print "end";

rofl = gtk.Builder();

rofl.add_from_file("mainwindow.ui");
win = rofl.get_object("mainWindow");
rofl.connect_signals("mainWindow");
win.show_all();
gtk.main();

Yet when I go to run that it complains that I am missing a handler for the mainWindow object.
I have tried doing rofl.connect_signals(win); as well

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2024-09-19 11:28:45

根据文档,< code>connect_signals 将映射或实例作为参数,并且

使用Python的内省功能
查看键(如果对象是
映射)或属性(如果对象是
一个实例)并尝试匹配它们
信号处理程序名称给出
接口说明。这
每个匹配引用的回调
键或属性连接到
他们的匹配信号。

因此,例如,当您传递 "mainwindow"(它是 str 的实例)时,属性是例如 upper等方法名称>lowerisalpha 等——与您可能感兴趣的任何内容无关。为什么您还需要 win 的属性来处理信号? IOW,您希望 connect_signals 能够做什么

可以在此 SO 问题 中找到更典型的示例用法本教程,其中提供了以下 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),该方法(通过introspection)将用作窗口销毁时引发的信号的处理程序。

Per the docs, connect_signals takes as the argument a mapping or instance, and

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.

So for example when you pass "mainwindow", which is an instance of str, the attributes are e.g. such method names as upper, lower, isalpha, and the like -- nothing to do with anything at all that you might be remotely interested about. And why would you want the attributes of win to handle signals, either? IOW, what do you expect connect_signals to do?

A more typical example use can be found e.g. in this SO question and this tutorial, which offers among others the following Python example:

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)     

as you see, here connect_signals is used in the typical way -- i.e., passing an object (self) with an on_window_destroy method that (by introspection) will be used as the handler for the signal raised on window destruction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文