Go to TogaWare.com Home Page. GNU/Linux Desktop Survival Guide
by Graham Williams
Duck Duck Go


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.

Counting the Words

Now we need to write some code to perform the counting of the words. This will not be much of a challenge though as we've already got the logic of the algorithm under control! Its now just a task of knowing the appropriate syntax.

Support further development by purchasing the PDF version of the book.
Other online resources include the Data Science Desktop Survival Guide.
Books available on Amazon include Data Mining with Rattle and Essentials of Data Science.
Popular open source software includes rattle and wajig.
Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 1995-2020 Togaware Pty Ltd. Creative Commons ShareAlike V4.