Final Panel Applet Example

The "Weekday" panel applet is not particularly useful or attractive, but it does show how a panel applet operates, and adds a few other features. Basically, Weekday displays the current day of the week (it's checked once an hour via a QTimer interrupt). It also contains a button which (if clicked) pops up the calendar for the current month. Here's the complete listing:

 
weekday.py
========

from kdeui import PyKPanelApplet, KPanelApplet, KDateTable, KMessageBox
from qt import QColor, QFrame, QTimer, QDate, QPushButton, SIGNAL, QLabel


# Any panel applet written in Python must include THIS "factory" function
# The name must be createApplet, and it must take the two args shown; it
# must return a PyKPanelApplet subclass
def createApplet (parent, configFile):
    actions = KPanelApplet.About | KPanelApplet.Help | KPanelApplet.Preferences
    return WeekdayPanelApplet (configFile, KPanelApplet.Normal, actions, parent, "weekday")


# Define the PyKPanelApplet subclass in a fashion similar to this
# (PyKPanelApplet is a subclass of KPanelApplet, and exposes all of
# KPanelApplet's methods)
class WeekdayPanelApplet (PyKPanelApplet):
    def __init__  (self, configFile, t, actions, parent, name, f = 0):
        PyKPanelApplet.__init__ (self, configFile, t, actions, parent, name, f)

        self.setPaletteBackgroundColor(QColor (0xff, 0xff, 0x00))
        self.setFrameStyle( QFrame.StyledPanel | QFrame.Sunken )

        self.timer = QTimer (self)

        # once an hour
        self.timer.start (3600000)
        self.connect (self.timer, SIGNAL ("timeout ()"), self.slotTimeout)


        self.dayLbl = QLabel (self)
        self.dayLbl.setGeometry (7, 2, 30, 18)

        self.calBtn = QPushButton (self)
        self.calBtn.setGeometry (5, 16, 20, 10)
        self.connect (self.calBtn, SIGNAL ("clicked ()"), self.slotClicked)

        # initialize
        self.slotTimeout ()

    def widthForHeight (self, h):
        return h

    def heightForWidth (self, w ):
        return w

    def slotTimeout (self):
        self.dayLbl.setText (QDate.shortDayName (QDate.currentDate ().dayOfWeek ()))

    def slotClicked (self):
        self.dt = KDateTable (self.parent ())
        self.dt.setGeometry (400, 600, 300, 130)
        self.dt.show ()

    def about (self):
        KMessageBox.information (self.parent (), "About not implemented", "weekday")

    def help (self):
        KMessageBox.information (self.parent (), "Help not implemented", "weekday")

    def preferences (self):
        KMessageBox.information (self.parent (), "Preferences not implemented", "weekday")

First, notice that some actions have been added in the createApplet function - specifying these actions in the constructor places associated entries in the menu that pops up when you right-click the applets "handle". At the end of the listing are the 3 slot methods that are connected (automatically) to the menu entries. In this case, the slots do nothing but pop up a "Not Implemented" KMessageBox.

Next, notice that there is no KApplication instance or event loop within the Weekday program. Weekday gets most of its events (except the timer interrupt programmed in) from somewhere else - probably kicker. In general, writing a panel applet is similar to writing a dialog or creating a custom widget. Since PyKPanelApplet is a descendant of QFrame, it has all of QFrame's (and QWidget's) functionality. You can, for example, add context menus to the applet itself or do anything else you can fit in the small amount of space the panel provides for applets.

Applets written using Python/PyKDE are large memory consumers, particularly if you don't have other PyKDE or PyQt apps running. If you are running other PyQt/PyKDE apps, most of the memory usage will be in shared libs.

If you test this app with the Panel Applet Installer, you'll find the calendar popup doesn't display very well. In addition, no mention has been made of config files with panel applets. You can probably find sufficient information in the KDE Class Reference docs to figure this out, or use your own method for creating and managing configuration files.

There is also a C++ tutorial on panel applets at this location.