This comp.lang.python post might be of some interest to those working on or with Ruby/GTK. (Original had HTML; trying to send text only.) Mitch Chapman wrote: > Mafee wrote: > > Can anyone give me some clues of linking python code to a GUI interface > > generate by Glade or GTK or whatever please? > > Here's an example. Two files are attached. > > GladeBase.py provides two base classes. Class UI creates a widget > hierarchy from a Glade file. Class Controller provides an > MVC-style controller for the UI. The main() function of the module > shows how to use these base classes to build and control a GUI. > > ui.glade is a simple user interface created using Glade. If you > plop it into the same directory with GladeBase.py and then run > GladeBase.py, you should get an application window on your display. > > The cool part about James Henstridge's libglade, on which this example > is based, is the way it wires up callback routines. You pass it > a dictionary of callable objects, keyed by name. It roots > through the Glade-based user interface hierarchy, and for every > connected signal declared in the interface, it looks for a callable > of the same name in the callback dictionary. If it finds one, > it plugs it in. It's cheap and easy. > > In the example, Controller._connectToUI() assumes that you will > derive from class Controller and define all of the callback methods > in the derived class. > > Hoping this helps... > > -- > Mitch Chapman > chapman / bioreason.com > > ---------------------------------------------------------------- > #!/usr/bin/env python > """This is an example of accessing Glade-built UIs from Python.""" > import gnome.ui, gtk, libglade > > class UI(libglade.GladeXML): > """Base class for all UIs loaded from glade.""" > def __init__(self, filename, rootname): > """Initialize a new instance.""" > libglade.GladeXML.__init__(self, filename=filename, root=rootname) > self.root = self.get_widget(rootname) > > def show(self): > """Make the UI visible.""" > self.root.show() > > class Controller: > """Base class for all controllers of glade-derived UIs.""" > def __init__(self, ui): > """Initialize a new instance. `ui' is the GladeXML UI to control.""" > self.ui = ui > self._connectToUI() > > def _connectToUI(self): > """Wire up self to its UI. > > This method assumes that any method defined by self's class > could be a Gtk+ callback. It wires up any methods referenced by > self's ui.""" > names = dir(self.__class__) > d = {} > for name in names: > d[name] = getattr(self, name) > > self.ui.signal_autoconnect(d) > > def show(self): > """Show the user interface.""" > self.ui.show() > > def main(): > """Module mainline (for standalone execution)""" > class SampleController(Controller): > def on_pushBtn_clicked(self, *args): > """Called when the 'push me' button is clicked.""" > print "Thanks for pushing me." > gtk.mainquit() > > def on_win_delete_event(self, *args): > """Called when the window is deleted.""" > gtk.mainquit() > > theUI = UI("ui.glade", "win") > theController = SampleController(theUI) > theController.show() > gtk.mainloop() > > if __name__ == "__main__": > main() > > ---------------------------------------------------------------- > CreateWorkspacecreate_workspacesrcpixmapsCTrueTrueGtkWindowwindelete_eventon_win_delete_eventWed, > 03 May 2000 00:16:13 > GMTGTK_WINDOW_TOPLEVELGTK_WIN_POS_NONEFalseFalseTrueFalseGtkVBoxvbox1False0GtkLabellabel1Go > on, push the > button.GTK_JUSTIFY_CENTERFalse0.50.5000FalseFalseGtkButtonpushBtnTrueclickedon_pushBtn_clickedMon, > 22 May 2000 15:28:00 GMTPush Me0FalseFalse -- Conrad Schneiker (This note is unofficial and subject to improvement without notice.)