Published on (
 See this if you're having trouble printing code examples

GNOME Panel Applets

by Daniel Solin

Greetings Pythonists and fans of British humor. Today, we're going to take a look at a great little Python module called PyGNOME. With this piece of software in hand, we can, without fear of being forced to use some other uncomfortable programming language, dive into the big world of GUI (Graphical User Interface) programming. You see, PyGNOME gives us a complete Python interface for dealing with the GNOME (GNU Network Object Model Environment) development/desktop environment. In this article, we will take a closer look at something called panel applets, which are essential elements on the GNOME desktop. Panel applets are usually developed in C, but this article will teach you how to use PyGNOME to develop GNOME panel applets with Python!

What is GNOME?

This question may sound trivial to some of you, but the answer is not that obvious. Most people think of GNOME as a modern desktop environment. While that is true, GNOME is much more than a desktop environment; it is also a complete framework for GUI development. The GNOME development framework provides libraries and components that programers can use to easily build standardized GUI applications and utilities. And, in addition to a desktop environment and GUI development standard, GNOME is also a large set of free software, including everything from word processors to CD players. All these software components follow the GNOME standard, and they therefore look and act basically the same.

What are GNOME panel applets?

The GNOME desktop comes with the panel, the bar that is located at the bottom of your GNOME desktop. From the panel, you can access your menus and quick-launchers. In addition, the panel can hold small programs, usually referred to as applets. Applets are programs designed to run inside another program. They are mini-applications. GNOME panel applets run on the panel instead of in a window of their own. If, however, you ignore the difference in where they are located, panel applets are just like any other program. Everything you can implement in a "real" GNOME program can also be implemented in a panel applet. (Although, when developing an applet you will have a much smaller area of the screen to work with).

So, why would you want to develop applets instead of real GNOME applications? Well, this depends on what kind of program you want to create. If you're out to develop an advanced office suite a la StarOffice, or a spreadsheet program like Microsoft Excel, panel applets are not for you! On the other hand, if you want to develop a CD or MP3 player, and you want to be able to control this program all the time, no matter what other programs you're running, you should probably do it as a panel applet. This is the whole idea with panel applets: to have access to certain programs permanently, without having to bother about minimizing and/or maximizing other windows. The panel is always visible, and therefore so are the applets on the panel.

Figure 1. Taskbar applet with the Gimp currently running.

Figure 1. Taskbar applet with the Gimp currently running (click for full view).

Figure 1 shows a typical GNOME panel. The elements on this panel are, from left to right, the GNOME menus, four launchers, the pager applet, the taskbar applet (with The Gimp currently running), and the date/time applet (currently showing the time 12:54 PM).

What we want to do is to create a program that resides on the panel, just as the pager, taskbar, and date/time applets do in Figure 1.

Installing PyGNOME

We, however, don't just want to develop applets; we want to do it with Python. For this, we need to install the PyGNOME package. PyGNOME gives you access to the GNOME development libraries through Python. We will start out by downloading and installing PyGNOME on your system.

You can get PyGNOME here. At the time of this writing, the latest version was 1.0.53. When the download is complete, go to the directory where you saved the file and issue the following command:

tar xvfz gnome-python-1.0.53.tar.gz

This will extract the files and place them in a directory called gnome-python-1.0.53. Now, go into this directory and run the configure script (which will make sure PyGNOME is compiled to suit your system):

./configure --prefix=/usr

configure now starts to examine your system, making sure everything is in place. Note that you need to change the --prefix option to suit your system. However, /usr should be correct for most systems (like Red Hat Linux). If configure exits with an error message, the problem is most likely a missing package. Then you just need to install that package and try again. For example, if configure reports it can't find the Python headers, you need to install the python-devel package.

Anyway, when configure exits without any errors (the last line should read creating config.h), you are ready to start the compilation. This is done by simply running make, as follows:


The compilation will now start. This can take a while, especially if you're on a slow system with not much RAM. After compiling, it's time to start the installation. (If you did not perform the last few commands as the superuser, you must change to your superuser or root account for the next command to work.) Use the following command to install your compiled package:

make install

You should now have PyGNOME installed under /usr/lib/python1.5/site-packages, and we're ready to start our panel applet production!

Your first PyGNOME applet

We will create a very simple panel applet to give you a view of how applets should be created. This example applet shows a label with a frame around it. This probably won't give you any revolutionary ideas of how panel applets can be used, but you'll learn the PyGNOME basics. With a basic understanding of "usual" Gtk+ and/or GNOME programming, you won't have any problem extending this example. Let's take a look at the example program:

1:  # Import the GNOME applet routines:
2:  import gnome.applet
4:  # Import the Gtk+ routines:
5:  import gtk
7:  # The main() function:
8:  def main():
9:	# Create an instance of the AppletWidget class:
10:	myapplet = gnome.applet.AppletWidget("Simple Applet")
12:	# Create a label to be shown inside the applet:
13:	label = gtk.GtkLabel("O'Reilly")
15:	# Create a frame:
16:	frame = gtk.GtkFrame()
18:	# Add the label to the frame:
19:	frame.add(label)
21:	# Show the label:
24:	# Show the frame:
27:	# Add the frame to the applet:
28:	myapplet.add(frame)
30:	# Set the tooltip text for the applet:
31:	myapplet.set_tooltip("This is a very simple panel applet.")
33:	# Show the applet:
36:	# Get into the Gtk main-loop:
37:	gtk.mainloop()
39: # This is plain old Python, so you should understand this:
40: if __name__ == '__main__': main()

If you have any GUI programming experience, the comments in the listing are probably enough for you. However, let us go through the example anyway, to make sure everything is clear.

To begin with, we import the GNOME applet and Gtk+ routines at lines 2 and 5. We do this, as you probably understand, to get access to the GNOME and Gtk+ routines that we want to use. The heart of every GNOME panel applet is the AppletWidget object. An object of this class represents the panel applet in whole; it is this object that makes our program a panel applet, and not a usual "window-program." The AppletWidget instance of this example is created on line 10 and is called myapplet. Also note that we pass an argument to the AppletWidget constructor. This is a string representing the name of the applet. Line 13 creates a GtkLabel instance, (label), and inserts the text "O'Reilly" into it. Line 16 creates a GtkFrame object, (frame). By inserting the label into the frame on line 19, we get a nice frame around the text (no kidding!!). The calls at lines 22 and 25 show the label and the frame on screen. Line 28 makes the frame a child of the AppletWidget object. Since label is a child of frame, label will automatically become a child of myapplet.

On line 31, we set the so-called "tooltip" for our applet. The tooltip is the text that is shown if you hold the mouse pointer over the applet for a while. A tooltip is usually a short description of the program in question (as in this case). On line 34, we make the applet visible, and on line 37, we get into the Gtk+ main loop, which will take care of all user interaction.

To save yourself some typing, download the example from here. Make sure you save it in a safe place.

Testing the example applet

Figure 2. A simple applet running on the GNOME panel.

Figure 2. A simple applet running on the GNOME panel (click for full view).

It's time to test our new, unbelievably advanced and sophisticated (ehrm...) applet. To start it, you do as you would with any other Python program:


If everything is in place, the applet should now start and be visible on your panel. Of course, you need to have the GNOME panel running for this to work. Figure 2 shows our simple applet running on a GNOME panel.

Figure 3. The standard applet-menu.

Figure 3. The standard applet-menu (click for full-size view).

Our applet is the "O'Reilly" label with the frame around it, between the taskbar and the date/time applet. It has no functions or features, but it's our applet! However, all applets have a standard applet-menu. To see this, click with your right mouse button somewhere on the applet. From the menu that now pops up, you can remove or move the applet and also add other applets to the panel. This menu is also shown in Figure 3.

To bring up the tooltip, hold your mouse pointer over the applet for a little while. A tooltip like the one shown in Figure 4 should show up.

That was that, congratulations to your first PyGNOME applet!


Figure 4. The tooltip menu.

Figure 4. The tooltip menu.

This article has taught you the basics of GNOME panel applet development with Python and the PyGNOME module. Although we have only touched the surface of this quite large subject, you should now have a basic understanding of what GNOME panel applets are, and when/why they are good.

When developing applets, it's important to remember that you can implement everything in an applet that you can in a "real" GNOME program; every GNOME/Gtk+ widget or feature can be used in an applet. Remember, a good panel applet can do everything a larger program can, it will just have the advantage of always being accessible. Did anyone say size matters? C'mon, get serious!

Daniel Solin is a freelance writer and Linux consultant whose specialty is GUI programming. His first book, SAMS Teach Yourself Qt Programming in 24 hours, was published in May, 2000.

Discuss this article in the O'Reilly Network Python Forum.

Return to the Python DevCenter.


Copyright © 2009 O'Reilly Media, Inc.