[PyKDE] Creating a Panel Applet

Jim Bublitz jbublitz at nwinternet.com
Tue Apr 8 18:31:01 BST 2003


On 08-Apr-03 POYEN OP Olivier (DCL) wrote:
> I'm trying to create an applet for kicker in python, as the title
> might have suggest.  For now, I'm following KDE Tutorials "How
> to add a panel applet", and I'm stucked by the fact than in the
> file myPanel.desktop, the line X-KDE-Library=Libname has to be
> some DSO, some dynamic librairie. Is there any way i can use a
> python script instead ? 

The short answer is "no - not directly". See below.

> I tried to have
> myPanel.desktop
> Exec=myPanel
> 
> where myPanel is a python script in my path.
> But what to put in that script ? 
> 
> I try to re-produce the factory describe in the tutorial:
> ----myPanel in PATH-------
> 
>#!/usr/bin/env python
> import kdeui
> import sys
> import kdecore
> 
> class myPanel(kdeui.KPanelApplet):
>       def __init__(self,*args):
>               kdeui.KPanelApplet.__init__(self,*args)
>               #I knwo that next line probably won't work, but
anyway....
>               self.setBackgroundColor('blue')
>       
> def init(*args):
>       return myPanel(*args)
 
> ----------
 
> Event if I know nothing about C++, I figured that there should be
> a X-KDE-Library= libname in the desktop file, and that this
> library should implement an init method.
 
> But how to render that in python ? 
 
> Do I stilll have to do what I want in C++, or can I create a part
> or the whole panel applet in python.  And how to make it appears
> on the screen ! 
 
> Any help or pointer or RTFM would be appreciated. 

I've looked into this once before in response to a question on this
list (and promised to do something about it, which I haven't done
yet). Without looking at the details again, I seem to recall that
panel applets are basically loaded by kicker as plugins, and in KDE
plugins are .so libs. I don't believe you can create .so libs
directly from Python.

There are a number of problems you need to solve (if I'm
remembering this correctly):

1. You need to create a .so lib so the applet can be loaded

2. You (probably) need to pass some C++ data or objects to the
Python applet script.  I'm not completely sure about this, but you
probably need to give the applet something like
KApplication/KMainWindow info or a pointer to some "top level"
QWidget. That relates to "how to make it appear on the screen" and
also not segfault.

3. You need to get the Python interpreter running (the
'/usr/bin/env python' trick won't work as far as I know, although
you might be able to fork it somehow); what I've done in this case
for plugins is make the Python interpreter a plugin too, and have
the .so lib from (1) load the interpreter plugin when the .so lib
from (1) loads. The first plugin then passes the script to the
interpreter plugin, which runs the script.

4. While operationally not a problem, you need to be aware that the
minimum size for your applet is going to be somewhere around 10MB
(the bindings libs for qt, kdecore and kdeui at a minimum - the
other libs you need will already be loaded). It seems like a lot for
a little thing stuck in the panel (eg something like 'biff'), but
memory is getting pretty cheap.

There is no easy solution to (4). (1) - (3) actually aren't that
hard to do once you figure out how to hook into kicker/KDE, but
require some C++ code, although not very much. I believe there's a
way to do this in your specific case, and it's likely there's a way
to create a general purpose solution, but I haven't been able to
find the time to get back to this.

All I can promise is that I'll try to find some time to take a look
at the problem. I have some related stuff I need to get to, so I
might actually do it in the near future, but I really can't promise
any delivery date. 

The other alternative you can consider is to put an icon for your
app in the system tray (similar to KMixer or KOrganizer). PyKDE
implements KSystemTray and there is a small example (systray.py) in
the PyKDE-3.5-1 release.

If you want to look into this more, look into the KParts and
KLibLoader KDE docs and tutorials.

Jim




More information about the PyQt mailing list