GNU/Linux Desktop Survival Guide by Graham Williams |
|||||
Using Libglade with Python |
Using with python is an excellent option for rapid prototyping and even build full strength applications. Like the use of in C there is never any need for you to convert the .glade file (containing the XML description of the interface) into Python code. Instead, reads the XML and directly builds the interface using the library (written in C but with an interface for Python).
The basic code is:
#! /usr/bin/env python import gtk import libglade import gnome.ui def init_app (): "Initialise the application." global wTree wTree = libglade.GladeXML ("gwords.glade", "app1") dic = {"on_quit_button_clicked" : gtk.mainquit, "on_exit1_activate" : gtk.mainquit} wTree.signal_autoconnect (dic) def main (): init_app () gtk.mainloop () if __name__ == '__main__': main () |
Here we have linked the interface callback on_exit1_activate to the library callback gtk.mainquit. The on_exit1_activate is associated through Glade with the Exit menu item and is supplied by default by the Gnome Application Window.
Save this code into a file called gwords.py in your /home/guest/Projects/gwords directory. Make the file executable with chmod u+x gwords.py. Then run the program with ./gwords.py. Your interface should come to life.
Problems arise if you don't have the appropriate packages installed. At a minimum make sure you have python-gnome installed (this package will depend on various other python and gnome packages which should be automatically installed by choosing to install python-gnome if you are using Debian).
You may also need to ensure that the PYTHONPATH environment variable includes the gnome and gtk modules, but check this only if you have problems running your Python program.