Python, Glade, & GTK

Mitch Chapman chapman at bioreason.com
Mon May 22 11:59:32 EDT 2000


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 at bioreason.com
-------------- next part --------------
#!/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()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20000522/42a6472d/attachment.html>


More information about the Python-list mailing list