Pigt Glade question: why does this simple script work?
I was writing to write a small pygtk application using a clearing to put together user interfaces. I've already created some windows that work, but somehow it doesn't work. I am getting the following traceback:
Traceback (most recent call last):
File "test.py", line 7, in <module>
class TestClass:
File "test.py", line 10, in TestClass
self.wTree.signal_autoconnect(self)
NameError: name 'self' is not defined
Here is the content of test.py:
#!/usr/bin/env python
import pygtk
import gtk
import gtk.glade
class TestClass:
def __init__(self):
self.wTree = gtk.glade.XML("test.glade")
self.wTree.signal_autoconnect(self)
def on_TestClass_destroy(self, widget, data):
gtk.main_quit()
if __name__ == "__main__":
window = TestClass()
gtk.main()
And here is the glade file, test.glade:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
<!--Generated with glade3 3.4.5 on Fri Nov 21 08:53:53 2008 -->
<glade-interface>
<widget class="GtkWindow" id="TestWindow">
<property name="visible">True</property>
<property name="title" translatable="yes">Test Window</property>
<signal name="destroy" handler="on_TestClass_destroy"/>
<child>
<placeholder/>
</child>
</widget>
</glade-interface>
The weird thing is that if I choose to call signal_autoconnect (self), a window opens. But if I replace this call with "self.on_TestClass_destroy (self, None, None)" instead, it returns the same NameError exception.
I really don't understand why this doesn't work as I have created several other window classes that work great.
Is the following code for someone here?
source to share
This code and window and signal connection work here.
There is a small bug though when calling the signal handler. The signal handler does not need to have a data argument, since only the widget is passed as an argument.
def on_TestClass_destroy(self, widget):
gtk.main_quit()
The data argument is only those provided for the connection if you need additional state for the signal handler.
source to share