繁体   English   中英

Glade实例没有属性

[英]Glade instance has no attribute

今天是个好日子,

我目前在Glade工作,为我已经编写的程序构建GUI。 我已经设置好窗口并正常运行,现在正尝试为单击按钮创建定义以开始执行我的代码。 但是,我不断收到以下错误代码:

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文件或其他内容中),但是看着它,我确保设置了clicked button signal,这是指出:

<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方法映射,则它应该可以工作。 我不确定在创建这样的类时是否有必要连接您的信号,但是总的来说,我认为这是一种好习惯。

我想到了。 通过将按钮的def移到dictionary和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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM