Python DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


GNOME Panel Applets
Pages: 1, 2, 3

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:

make

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
3:  
4:  # Import the Gtk+ routines:
5:  import gtk
6:  
7:  # The main() function:
8:  def main():
9:	# Create an instance of the AppletWidget class:
10:	myapplet = gnome.applet.AppletWidget("Simple Applet")
11:
12:	# Create a label to be shown inside the applet:
13:	label = gtk.GtkLabel("O'Reilly")
14:
15:	# Create a frame:
16:	frame = gtk.GtkFrame()
17:
18:	# Add the label to the frame:
19:	frame.add(label)
20:
21:	# Show the label:
22:	label.show()
23:
24:	# Show the frame:
25:	frame.show()
26:
27:	# Add the frame to the applet:
28:	myapplet.add(frame)
29:
30:	# Set the tooltip text for the applet:
31:	myapplet.set_tooltip("This is a very simple panel applet.")
32:
33:	# Show the applet:
34:	myapplet.show()
35:
36:	# Get into the Gtk main-loop:
37:	gtk.mainloop()
38:
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.

Pages: 1, 2, 3

Next Pagearrow





Sponsored by: