Glade 实例没有属性

Glade instance has no attribute

大家好,

我目前正在 Glade 工作,为我已经编写的程序构建 GUI。我已正确设置 window 和 运行,现在正尝试为按钮点击创建 defs 以开始执行我的代码。但是,我不断收到以下错误代码:

glade.py:17: RuntimeWarning: missing handler 'on_button4_clicked'
self.builder.connect_signals(self) 
glade.py:17: RuntimeWarning: missing handler 'on_button1_clicked'
self.builder.connect_signals(self)
Traceback (most recent call last):
File "glade.py", line 31, in <module>
main = mainClass()
File "glade.py", line 21, in __init__
dic = { "on_button4_clicked" : self.button4_clicked}
AttributeError: mainClass instance has no attribute 'button4_clicked'

这是我的 python 代码:

#!/usr/bin/env python

import gtk

class mainClass:

    def on_window1_destroy(self, object, data=None):
        gtk.main_quit()

    def on_gtk_quit_activate(self, menuitem, data=None):
        gtk.main_quit()

    def __init__(self):
        self.gladefile = "pyhelloworld.glade"
        self.builder = gtk.Builder()
        self.builder.add_from_file(self.gladefile)
        self.builder.connect_signals(self)
        self.window = self.builder.get_object("window1")
        self.window.show()

    dic = { "on_button4_clicked" : self.button4_clicked}

    self.wTree.signal_autoconnect(dic)

    def button4_clicked(self, widget):
        print "Hello World!"

if __name__ == "__main__":
main = mainClass()
gtk.main() 

我没有附上我的 .glade 代码,因为它很长(有没有办法将它附加到 zip 文件或其他东西中)不过看着它,我确保设置了点击按钮信号,这里是声明的部分:

<object class="GtkButton" id="button4">
<property name="label" translatable="yes">VDBench Right Slot</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_button4_clicked" swapped="no"/>

如果有人能提供帮助,我将不胜感激,因为我是 python 的新手,甚至是 GUI 设计的新手。谢谢

你应该不需要打电话给

self.wTree.signal_autoconnect(dic)

如果您改为在 init() 方法中声明此字典

signal_dictionary = {"on_button4_clicked" : self.button4_clicked}

在你打电话之前

self.builder.connect_signals(signal_dictionary)

如果您提前在 signal_dictionary 字典中定义所有空地信号 >>> python 方法映射,它应该可以工作。我不确定是否有必要像这样在创建 class 时连接您的信号,但总的来说,我认为这是一个很好的做法。

我明白了。通过将按钮的 def 移动到字典和 connect_signals 之前,它现在是正确的 运行。感谢您的协助,现在是最终代码:

#!/usr/bin/env python

import gtk

class mainClass:
    def button1_clicked(self, widget):
        print "Hello World!" 
    def button2_clicked(self, widget):
        print "Hello World!" 
    def button3_clicked(self, widget):
        print "Hello World!" 
    def button4_clicked(self, widget):
        print "Hello World!" 
    def button5_clicked(self, widget):
        print "Hello World!" 
    def button6_clicked(self, widget):
        print "Hello World!" 


    def on_window1_destroy(self, object, data=None):
        gtk.main_quit()

    def on_gtk_quit_activate(self, menuitem, data=None):
        gtk.main_quit()

    def __init__(self):

        self.gladefile = "pyhelloworld.glade"
        signal_dictionary = { "on_button1_clicked" : self.button1_clicked,
                      "on_button2_clicked" : self.button2_clicked,
                      "on_button3_clicked" : self.button3_clicked,
                      "on_button4_clicked" : self.button4_clicked, 
                      "on_button5_clicked" : self.button5_clicked, 
                      "on_button6_clicked" : self.button6_clicked} 
        self.builder = gtk.Builder()
        self.builder.add_from_file(self.gladefile)
        self.builder.connect_signals(signal_dictionary)
        self.window = self.builder.get_object("window1")
        self.window.show()   

if __name__ == "__main__":
    main = mainClass()
    gtk.main()