I've created a simple helper class for Glade, a widget designer for the crossplatform GTK toolkit:
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
class GladeApplication:
def __init__(self, glade_file):
class Widgets:
def __init__(self, glade):
self._glade = glade
def __getattr__(self, name):
return self._glade.get_widget(name)
self._glade = gtk.glade.XML(glade_file)
self._glade.signal_autoconnect(self)
self.widget = Widgets(self._glade) This class puts all the widgets defined by Glade in "self.widgets", and looks for events in the main class.
For instance, to test this I created a simple ROT13 application that consists of a window, a textview widget, and a button widget. In Glade I setup the window's destroy event to point to "on_destroy", and the button clicked event to point to "on_button_clicked". I saved the glade XML file as "rot13.glade".
The python code to use this:
class Rot13(GladeApplication):
def __init__(self):
GladeApplication.__init__(self, "rot13.glade")
def on_destroy(self, window):
gtk.main_quit()
def on_button_clicked(self, button):
text_buffer = self.widget.textview.get_buffer()
text = text_buffer.get_text(*text_buffer.get_bounds())
text_buffer.set_text(text.encode('rot13'))
Rot13()
gtk.main()