Search code examples
pythonbuttonclickinstanceglade

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. 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:

#!/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:

<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. Thanks


Solution

  • You should not need the call to

    self.wTree.signal_autoconnect(dic)
    

    if instead you declare this dictionary in your init() method

    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. 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.