Writing Panel Applets Using PyKDE

PyKDE includes several demo applets that are not very useful. Here's the first - the smallest possible Python/PyKDE panel applet (a total of 6 lines of code, but does absolutely nothing):

demo0.py
========

 1| from kdeui import PyKPanelApplet, KPanelApplet
 2|
 3| def createApplet (parent, configFile):
 4|    return Demo0PanelApplet (configFile, KPanelApplet.Normal, 0, parent, "demo0")
 5|
 6| class Demo0PanelApplet (PyKPanelApplet):
 7|    def __init__  (self, configFile, t, actions, parent, name, f = 0):
 8|        PyKPanelApplet.__init__ (self, configFile, t, actions, parent, name, f)
Going through it line by line:

If you install this applet and then use kicker to load it, nothing will occur except placing a "handle" for the applet on the panel (you can right click the handle to pop up a menu to delete the applet from the panel).

We won't do any more with the 'createApplet' factory function - it's only job is to be a well-known entry point for loading the actual applet. A slightly (but not much more) useful applet might look like this:

demo.py
========
	(createApplet not shown)
	
 1| class DemoPanelApplet (PyKPanelApplet):
 2|    def __init__  (self, configFile, t, actions, parent, name, f = 0):
 3|        PyKPanelApplet.__init__ (self, configFile, t, actions, parent, name, f)
 4|        self.setPaletteBackgroundColor(QColor (0x00, 0x00, 0xff))
 5|        self.setFrameStyle( QFrame.StyledPanel | QFrame.Sunken )
 6|
 7|    def widthForHeight (self, h):
 8|        return h
 9|
10|   def heightForWidth (self, w ):
11|       return w

The 'set' method calls in __init__ just set the style of the applet widget and paint it blue. The 'widthForHeight' and 'heightForWidth' methods are used by kicker to size/resize the applet depending on its orientation (the absence of these is why the first listing did nothing at all). When the kicker panel is horizontal, all applets have the same height constraint. kicker then calls 'widthForHeight' with the value of the panel's height, and lets the applet tell kicker how wide it would like to be. In line 8, the applet just returns the height value passed in, so the applet will be square. When the kicker panel is positioned vertically (at the sides of the screen), kicker fixes the applet width and allows the applet to request a height (using 'heightForWidth').

The applet shown above will put a blue square in the panel if loaded. You can play around with colors, styles and width/height ratios if you want to be convinced it actually works.

Panel Applet Logistics

The above demonstrate that beyond the problems of writing a program that does something useful, very little additional work is necessary to create a panel applet UNLESS you actually want to install the applet and see it work. It's not hard to do that, but there are a few details to keep straight. PyKDE also includes a Panel Applet Installer, which makes life a lot easier. The next page detours into considerations of panel applet debugging and installation, before returning to a couple more examples that extend the functionality beyond putting a colored rectangle of arbitrary size on the panel.