Glade, PyGTK, gnome-python Textorial

Setting up Lib Glade Environment in Python

Once again, I am going to take the liberties of assuming that you have your packages set up and configured correctly, and that any modules that you need will import correctly. If you find that some modules are importing incorrectly, it could be due to an install in /usr/local/lib rather than /usr/lib, or vice versa depending on how your system is set up.

We will now set up the trivial case for a glade project with gnome components.

FILE: simple.py -rwxr-xr-x [200b]


1: #!/usr/bin/env python
2: import gtk
3: import gnome.ui
4: import gtk.glade
5:
6: # necessary for gtk.glade.XML() with gnome widgets
7: gnome.init("app1", "1.0")
8: widgetTree = gtk.glade.XML("project1.glade")
9: gtk.main()

When run on the command line, the program should output something like the window below.

First Run!

Well, now that we know that it works, lets investigate a little and see how it works. The first four lines shouldn't be anything grand for any python programmer, but for those who are just getting started, they set up python to interprate the code and then import 3 modules.

If you are really interested in what is going on under the hood of these modules, and what other functions you can use with these modules, you can inspect the files themselves, join the devel list for the respective projects, or read their documentation. The documentation for PyGTK is fairly good, and for our purposes covers pretty much anything we'd need to know.

Before we go any further, a brief explanation of glade, libglade, and how python and pygtk fit into the equation. Glade is a interface tool designed for C/C++ development for GTK+1.x and 2.x; not exactly meant for python. It has an integrated code-creation and build routine subsystem to make it easy to generate and modify glade-created ui's in the form of C header files. Libglade was developed by James Henstridge as a library to parse the xml output of glade's ".glade" save files and dynamically build interfaces at runtime. What this means for the developer in C land is that he does not have to recompile everytime he makes a change to the UI; if he's changing a mere default condition or alignment, he can just change, save, and then run; since the XML file is parsed at run time, the new changes will be in effect.

What this means for the python programmer is essentially nothing if it were not for the other creations of Henstridge: gnome-python and PyGTK; gnome and gtk bindings for python. These wrappers along with libglade allow python programs to create a complex widget tree from a glade XML file and create interfaces at runtime. We'll get into widget tree's later on as we explore more of glades own interface.

gnome.init()

Back to the last 3 lines of the program. The first one:

gnome.init("app1", "1.0")

initializes the gnome.ui system. This prepares the system (vague, I know) for the gnome widgets we will be using with our glade interface. After the short aside on glade and libglade, the next line should almost be self explanatory:

gtk.glade.XML

widgetTree = gtk.glade.XML("project1.glade")

This line creates a widget tree from the glade XML file "project1.glade". The widgets that it makes will all be accessable as PyGTK widgets (and objects, and labels, etc; all the way down the hierarchy) through get_widget() and other methods. After this line of code, our interface is waiting in the interpreter, waiting to enter (and subsequently sleep in) the main gtk loop; which is brought by the next line.

gtk.main()

All GTK programs spend most of their time sleeping in the gtk main loop, waiting for user interaction. When they receive a notification of user interaction (a signal or event, which is covered shortly in this tutorial), they wake up to perform whatever task it is that was assigned them for that event, and then sleep again. After this function is called, your GTK program is officially running and waiting for signals. Congratulations; pretty easy, no?

back to the Main App | forward to Signals...
up to the index