Python Glade GTK Egli
Python Glade GTK Egli
Christian Egli
What is RAD?
RAD (Rapid Application Development) is a programming system that enables programmers to build working
programs very quickly. In general, RAD systems provide a number of tools to help build graphical user interfaces that would otherwise take a large development effort. Two popular RAD systems for Windows are Visual
Basic and Delphi.
GNOME
GNOME [1] is well established as a desktop environment. Lesser known, it also features a development platform which offers a multitude of libraries based on standard and open technologies, such as CORBA and XML.
Using this platform allows the developer to write userfriendly and stable software easily and efficiently.
GNOME is part of the GNU Project [2]. More information about GNOME, the source code, binaries and documentation can be downloaded from www.gnome.org.
GNOME is built upon GTK+, a free multi-platform
toolkit for creating graphical user interfaces.
Scheme or Java. It includes a rich set of libraries for networking, standard database API and even XML-RPC for
example.
More information about Python, binaries and source code
can be obtained from www.python.org.
Glade
Glade [4] is the GUI-Builder for GNOME. It allows
straightforward and simple construction of Graphical
User Interfaces and gives the developer access to all of
the GTK+ and GNOME widgets.
LibGlade
Usually Glade is used to generate C or C++ code directly.
However, an XML representation of the GUI that was
created can also be generated. This XML file can be
loaded at runtime with the help of LibGlade [5] which
recreates the GUI as it was designed in Glade.
Hands-on Demo
By writing a small application gPizza for the configuration of the hackers favourite food, I will demonstrate
live how Python and GNOME can be used to create a
running prototype in a very short time.
Highlights of the demo will be:
creation of the GUI with Glade
Python
Python [3] is an interpreted, interactive, object-oriented
programming language. It is often compared to Tcl, Perl,
Voil`a, theres the combo box in the column header providing a glimpse into the enormous flexibility of GTK+.
Figure 5 shows the resulting GUI.
When we resize the main window, we can see that the behaviour has changed: the horizontal box with the check
buttons and the columned list are no longer expanded.
Getting immediate feedback from the GUI-Builder Glade
enables us not only to understand how the layout mechanism works but also to learn to work with it. More information about the layout philosophy of GTK+ can be
found in both the GTK+ Tutorial [6] and Havoc Penningtons excellent book [7].
Flexibility of GTK+
There is a product from an unnamed company located in
Redmond that happens to contain a lot of columned lists.
This product has a cute feature that allows the user to
Save
So far so good. Before we proceed to save our piece of
art we have to set a few things in the project options dialog. Our project is to be saved with the name gPizza
in the directory linuxtag2001/src. Figure 6 illustrates how this is done. Note we enable GNOME support
and (although C is selected by default) we do not need
to specify a programming language since we do not use
the code generation features of Glade.
Listing 1: gpizza.glade
<?xml version="1.0"?>
<GTK-Interface>
<project>
<name>gPizza</name>
<program_name>gpizza</program_name>
<directory></directory>
<source_directory>src</source_directory>
<pixmaps_directory>pixmaps</pixmaps_directory>
<language>C</language>
<gnome_support>True</gnome_support>
<gettext_support>True</gettext_support>
</project>
<widget>
<class>GnomeApp</class>
<name>app1</name>
<title>gPizza</title>
<type>GTK_WINDOW_TOPLEVEL</type>
<position>GTK_WIN_POS_NONE</position>
<modal>False</modal>
<allow_shrink>False</allow_shrink>
<allow_grow>True</allow_grow>
<auto_shrink>False</auto_shrink>
<widget>
<class>GtkCheckButton</class>
<name>checkbutton1</name>
<can_focus>True</can_focus>
<label>Mozzarella</label>
<active>False</active>
<draw_indicator>True</draw_indicator>
<child>
<padding>0</padding>
<expand>False</expand>
<fill>False</fill>
</child>
</widget>
<widget>
<class>GtkCheckButton</class>
<name>checkbutton2</name>
<can_focus>True</can_focus>
<label>Olives</label>
<active>False</active>
<draw_indicator>True</draw_indicator>
<child>
<padding>0</padding>
<expand>False</expand>
<fill>False</fill>
</child>
</widget>
#!/usr/bin/env python
2
3
4
5
6
7
8
9
10
11
def main():
xml = libglade.GladeXML(gpizza.glade)
app = xml.get_widget(app1)
app.connect("destroy", mainquit)
mainloop()
12
13
14
if __name__ == "__main__":
main()
First flight
Figure 7: First flight of gPizza
Now that the GUI is created and stored, we are able to
see how the user interface looks at runtime. For that
purpose we write a tiny little Python program that does
nothing more than load the XML file with the help of
LibGlade, and construct the GUI. After that it simply sits
in the mainloop waiting for user input. Listing 3 shows
the code.
More functionality
Lets have a closer look at the program: Line 1 is the standard beginning for a Python script. In lines 3-5 we import
the modules gtk, gnome, gnome.ui and libglade.
Lets run the program. A screenshot of this first invocation is shown in Figure 7. The menu and the buttons all
look beautiful but show precious little reaction to user input. This of course is due to the fact that so far we have
only connected the signal destroy of the main window
to a function. Indeed if we close the window the program
quits.
if __name__ == "__main__":
main()
Conclusion
We have shown that by combining the tools GNOME,
Python, Glade and LibGlade a working prototype can be
created very rapidly.
intervals. The language itself evolves very conservatively and changes are usually backward compatible.
References
[1] GNOME. http://www.gnome.org/.
[2] GNU Project. http://www.gnu.org/.
[3] Python. http://www.python.org/.
[4] Daemon Chaplin. Glade. http://glade.gnome.org/.
[5] James Henstridge.
Libglade Reference Manual.
http:
//developer.gnome.org/doc/API/libglade/
libglade.html.
[6] GTK+. http://www.gtk.org/.
[7] Havoc Pennington. GTK+/Gnome Application Development.
New Riders Publishing, 1999.
[8] Miguel de Icaza. Introduction to Bonobo. http://www.
ximian.com/tech/bonobo.php3, 1999.
[9] Bonobo.
http://developer.gnome.org/arch/
component/bonobo.html.
[10] Rodrigo Moya. GNOME-DB. www.gnome-db.org.
[11] James Henstridge. gnome-python. http://www.daa.com.
au/james/pygtk/.
[12] Hilaire Fernandes. Developing Gnome Application with Python.
http://www.linuxfocus.org/English/July2000/
article160.shtml.
[13] Deirdre Saoirse. The Ten Minute Total Idiots Guide to using Gnome/GTK+ & Glade with Python. http://www.
baypiggies.org/10mintig.html.
[14] Xianping Ge. Using Python + Glade. http://www.ics.
uci.edu/xge/python-glade/python-glade.html,
2000.
About the author: