pygtk Glade 问题:为什么这个简单的脚本不起作用?

发布于 2024-07-09 17:21:22 字数 1565 浏览 8 评论 0原文

我一直在使用glade 编写一个小型pygtk 应用程序来组合UI。 我已经创建了几个可以工作的窗口,但由于某种原因,这个窗口无法工作。 我得到以下回溯:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    class TestClass:
  File "test.py", line 10, in TestClass
    self.wTree.signal_autoconnect(self)
NameError: name 'self' is not defined

这是 test.py 的内容:

#!/usr/bin/env python

import pygtk
import gtk
import gtk.glade

class TestClass:
    def __init__(self):
        self.wTree = gtk.glade.XML("test.glade")
        self.wTree.signal_autoconnect(self)

    def on_TestClass_destroy(self, widget, data):
        gtk.main_quit()

if __name__ == "__main__":
    window = TestClass()
    gtk.main()

这是林间空地文件 test.glade:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
<!--Generated with glade3 3.4.5 on Fri Nov 21 08:53:53 2008 -->
<glade-interface>
  <widget class="GtkWindow" id="TestWindow">
    <property name="visible">True</property>
    <property name="title" translatable="yes">Test Window</property>
    <signal name="destroy" handler="on_TestClass_destroy"/>
    <child>
      <placeholder/>
    </child>
  </widget>
</glade-interface>

奇怪的是,如果我取出 signal_autoconnect(self) 调用,窗口就会打开。 但是,如果我用“self.on_TestClass_destroy(self, None, None)”替换该调用,它会返回相同的 NameError 异常。

我真的不明白为什么这不起作用,因为我已经创建了其他几个可以正常工作的窗口类。

以下代码对这里的任何人都有效吗?

I've been writing writing a small pygtk application using glade to put together the UIs. I've created several windows already that work, but for some reason this one isn't working. I get the following traceback:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    class TestClass:
  File "test.py", line 10, in TestClass
    self.wTree.signal_autoconnect(self)
NameError: name 'self' is not defined

Here is the contents of test.py:

#!/usr/bin/env python

import pygtk
import gtk
import gtk.glade

class TestClass:
    def __init__(self):
        self.wTree = gtk.glade.XML("test.glade")
        self.wTree.signal_autoconnect(self)

    def on_TestClass_destroy(self, widget, data):
        gtk.main_quit()

if __name__ == "__main__":
    window = TestClass()
    gtk.main()

And here is the glade file, test.glade:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
<!--Generated with glade3 3.4.5 on Fri Nov 21 08:53:53 2008 -->
<glade-interface>
  <widget class="GtkWindow" id="TestWindow">
    <property name="visible">True</property>
    <property name="title" translatable="yes">Test Window</property>
    <signal name="destroy" handler="on_TestClass_destroy"/>
    <child>
      <placeholder/>
    </child>
  </widget>
</glade-interface>

The strange thing is that if I take out the signal_autoconnect(self) call, the window opens. But if I replace that call with "self.on_TestClass_destroy(self, None, None)" instead, it returns the same NameError exception.

I really don't understand why this isn't working, as I've created several other window classes that work fine.

Is the following code working for anyone here?

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

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

发布评论

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

评论(1

夜吻♂芭芘 2024-07-16 17:21:22

该代码、窗口和信号连接在这里工作正常。

但在调用信号处理程序时有一个小错误。 信号处理程序不应有数据参数,因为只有小部件作为参数传递。

def on_TestClass_destroy(self, widget):
    gtk.main_quit()

数据参数仅是在连接时提供的参数,以防您需要信号处理程序的额外状态。

That code and window and signal connection work fine here.

There is a small bug though when calling the signal handler. The signal handler should not have a data argument, since only the widget is passed as an argument.

def on_TestClass_destroy(self, widget):
    gtk.main_quit()

The data argument(s) are only those provided on connect in case you need extra state for a signal handler.

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