Glade实例没有属性

时间:2016-03-14 17:48:18

标签: python button click instance glade

大家好日子,

我目前正在Glade工作,为我已编写的程序构建GUI。我已经设置好窗口并正常运行,现在我正在尝试为按钮点击创建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设计。感谢

3 个答案:

答案 0 :(得分:0)

您不应该需要致电

self.wTree.signal_autoconnect(dic)

如果您在 init ()方法

中声明此词典
signal_dictionary = {"on_button4_clicked" : self.button4_clicked}

之前打电话

self.builder.connect_signals(signal_dictionary)

如果您定义了所有的林间空地信号&gt;&gt;&gt;提前在signal_dictionary字典中的python方法映射,它应该工作。我不确定在创建这样的课程时是否有必要连接你的信号,但总的来说我认为这是一个好习惯。

答案 1 :(得分:0)

通过遵循您的指示,它确实修复了运行时警告,但它仍然提供了关于无属性的相同错误消息。这是我设置的方式,以防你有不同的意思:

self.gladefile = "pyhelloworld.glade"
signal_dictionary = { "on_button4_clicked" : self.button4_clicked}
self.builder = gtk.Builder()
self.builder.add_from_file(self.gladefile)
self.builder.connect_signals(signal_dictionary)

答案 2 :(得分:0)

我明白了。通过将按钮的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()