[英]Glade instance has no attribute
Good day everyone, 今天是个好日子,
I am currently working in Glade to build a GUI for a program that I have already written. 我目前在Glade工作,为我已经编写的程序构建GUI。 I have the window set up and running properly, and am now trying to create defs for the button clicks to start executing my code. 我已经设置好窗口并正常运行,现在正尝试为单击按钮创建定义以开始执行我的代码。 I keep getting the following error code however: 但是,我不断收到以下错误代码:
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'
Here is my python code: 这是我的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()
I have not attached my .glade code because it is rather long (is there a way to attach it in a zip file or something) Looking at it though, I made sure to set clicked button signal, and here is the portion that states that: 我尚未附加我的.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"/>
If anyone could help I would greatly appreciate it, as I am fairly new to python and even newer to GUI design. 如果有人可以提供帮助,我将不胜感激,因为我是python的新手,甚至是GUI设计的新手。 Thanks 谢谢
You should not need the call to 您不需要致电
self.wTree.signal_autoconnect(dic)
if instead you declare this dictionary in your init () method 相反,如果您在init ()方法中声明此字典
signal_dictionary = {"on_button4_clicked" : self.button4_clicked}
before you call 致电之前
self.builder.connect_signals(signal_dictionary)
If you define all of your glade signals >>> python method mappings in the signal_dictionary
dictionary ahead of time, it should work. 如果您提前在signal_dictionary
字典中定义了所有空转信号>>> python方法映射,则它应该可以工作。 I am not sure that it is necessary to connect your signals upon creation of the class like this, but in general I think it is good practice to. 我不确定在创建这样的类时是否有必要连接您的信号,但是总的来说,我认为这是一种好习惯。
I figured it out. 我想到了。 by moving the def of the button to before the dictionary and connect_signals, it is running properly now. 通过将按钮的def移到dictionary和connect_signals之前,它现在可以正常运行。 Thanks for your assistance here is the final code now: 感谢您的协助,这是现在的最终代码:
#!/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.