faqts : Computers : Programming : Languages : Python : Common Problems

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

9 of 10 people (90%) answered Yes
Recently 3 of 4 people (75%) answered Yes

Entry

How can I link python code to a GUI interface generated by Glade or GTK or whatever please?

May 23rd, 2000 04:11
unknown unknown, Mitch Chapman


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

---

#!/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()

---

CreateWorkspace create_workspace src pixmaps C True True GtkWindow win 
delete_event on_win_delete_event Wed, 03 May 2000 00:16:13
GMT GTK_WINDOW_TOPLEVEL GTK_WIN_POS_NONE False False True False GtkVBox 
vbox1 False 0 GtkLabel label1 Go on, push the
button. GTK_JUSTIFY_CENTER False 0.5 0.5 0 0 0 False False GtkButton 
pushBtn True clicked on_pushBtn_clicked Mon, 22 May 2000
15:28:00 GMT Push Me 0 False False