This article will cover how to make an applet for the GNOME 2.x desktop with the Python programming language. Usually, GNOME applets are made in C, which has the big advantage of generating compiled executables (this means less memory footprint and faster programs) but in the other hand it's more difficult to do it and it takes more time to write the code. Python programs are developed faster, they have fewer lines of code, and it's an object oriented programming language. But it's an interpreted language, so the execution speed can be worse. It's up to the reader to decide if it is worthy writing a GNOME applet in Python. One good aproach is to prototype the applet in Python and then write the final version in C. Here we'll explain the necessary steps to write and deploy a simple applet from scratch using Glade 2 and libglade for the dialogs. Look at the bibliography for an in-deep explanation. Another good source of information is the pyGTK mailing list archives.

However, with regard to the code shown at the tutorial, we advise you to better look at the example section, where you will find some releases that do like it has been explained here, so you can see it growing from a prototype to a really functional applet.

The simplest applet we can write can be found in the gnome-python package documentation (/usr/share/doc/python2.x-gnome2/examples/applet/applet.py in DEBIAN-based distributions) or simply in the gnome-python tarball from the original GNOME ftp.

First, we need to import some Python modules that we will need. The pyGTK module, needed to specify the GTK version used (we'll be using 2.x in this article), the gnome module, that contains all the useful classes and methods about the gnome desktop, i.e. the applet class, and the gtk module, Python bindings for the GTK toolkit.

Then, we define the sample factory function to generate applet objects, with a text label "Sucess!". This function receives the object to be initializated (the applet) and the bonobo activation ID that the new factory will implement. It's all that simple. It returns gtk.TRUE if no errors were reported (see the panelapplet reference manual for details). When we call to gnome.applet.bonobo_factory().

When we call the bonobo_factory method, we need to pass it the following arguments:

The second thing we need to get the applet running is to construct the bonobo server file. bonobo-activation-server, the GNOME application that tracks information about installed components and brokers components, reads and mantains the component descriptions from /usr/lib/bonobo/servers/*.server. These files provide an XML description of a component's capabilities which can be queried and manipulated by clients using the activation client library. Bonobo-activation-server, also ensures that the minimum neccessary number of servers for your display setup are running.

Bonobo activation server is nothing but a daemon implementing a set of CORBA interfaces. These CORBA interfaces implement a name service for the set of CORBA servers installed on your system. GNOME Object Activation Framework daemon (OAFD) knows about all the CORBA servers in your system, running or not. The OAF daemon will activate those servers if you ask for them. Each server is described by its .server file which contains among other things the IDL interfaces it implements, some specific properties and an IID (Implementation ID). Each IID has to be globally unique, and its format is pretty simple:

OAFIID:program_name:UUID

The bonobo factories are CORBA objects that allow the creation of other new CORBA objects. This is a common practice in GNOME. The use of factories will allow us to use only an executable to get several instances of the component.

So we need to define the .server file and place it at the location mentioned before:

The .server file registers two unique OAF identifiers, and give the activation-server the description of how the objects must be created. The first one is the Factory, that is created when the .py script is executed. The second is the applet itself, and is created asking the Factory how to do it. When we do the "add to panel" action in GNOME, it takes the OAF identifiers that are supposed to implement the "IDL:GNOME/Vertigo/PanelAppletShell:1.0" in its "repo_ids" attribute. The submenu is determined by the "panel:category" attribute, the icon from "panel:icon", the text displayed at the menu comes from "name" and the tooltip text from "description".

The sample_factory() function is called when a new applet is created. It receives a container that should be populated with the applet contents.