Glade Creates an XML Representation of the GUI

It should already be clear how powerful Glade is. With it, you can construct a GUI visually instead of by writing code. But how do you get this GUI into a representation that Python can understand?

Save your Glade project, and then look in the project's directory. You should find a GladeTwoButtonsGUl.glade file that contains an XML representation of the GUI you just created. That XML representation will look something like this (although a lot of it has been edited out for clarity):

<?xml version="1.0" standalone="no"?> <!--*- mode:

xml -*-->

<!DOCTYPE glade-interface SYSTEM

"http://glade.gnome.org/glade-2.0.dtd">

<glade-interface>

<widget class="GtkWindow" id="window!">

<child>

<widget class="GtkVBox" id="vbox!">

<child>

<widget class="GtkButton'

' id="button1">

<property name="label"

translatable="yes'

'>Hello World</property>

</widget>

</child>

<child>

<widget class="GtkButton'

' id="button2">

<property name="label"

translatable="yes'

'>Hello Again</property>

</widget>

</child>

</widget>

</child>

</widget>

</glade-interface>

If this looks like gibberish to you, consult Chapter 15 for more information on XML. If you can read XML, notice that this data structure defines a tree of tags that corresponds to the tree structure of the GUI. The interface as a whole contains a window (GtkWindow), which contains a vertical box (GtkVBox), which contains two buttons (GtkButton). The buttons have customized label properties, just as you defined them in Glade.

In short, this XML file contains the same information as the GUI we defined visually, and the same information as the several lines of Python we used to define the same GUI in TwoButtonsGUl.py. If there were a way to get Python to parse this file and create a GUI out of it, we could save a significant amount of code. This is where libglade comes in.

Try It Out Building a GUI from a Glade File libglade parses the XML file and makes GTK widgets corresponding to the widgets described in the XML file. Here's GladeTwoButtonsGUI.py, a version of TwoButtonsGUl.py that loads its GUI from the XML file instead of using a series of Python statements:

#!/usr/bin/env python

import findgtk

import gtk.glade

class TwoButtonsGUI:

def _init_(self):

self.window = gtk.glade.XML('GladeTwoButtonsGUI.glade',

'windowl')

if _name_ == '_main_':

TwoButtonsGUI()

gtk.main()

This program uses libglade to load a set of GUI widgets from the GladeTwoButtonsGUI.glade file we created with Glade. The GUI looks just the same as the Glade mock-up, and the same as we created with pyGTK calls in the TwoButtonsGUl.py program. The advantage over the original TwoButtonsGUl.py is that we had to write a lot less code to get the same GUI.

Glade greatly simplifies the layout of even small GUIs. As you'll see, it also provides a framework for designating which events a GUI is expected to handle.

You may need to install the Python libglade bindings separately from Glade. If all else fails, you can download the bindings as part of the pygtk package, at http://ftp.gnome.org/pub/GNOME/ sources/pygtk/. See Appendix B for more information.

Was this article helpful?

0 0

Post a comment