June 01, 2009

Python Plasmoid Example

« Mycila Plugin Framework | Main | Trails Framework Example »

The new plasma engine of KDE 4.x introduced plasmoids. Plasmoids are small dialogs which are embeddable within the KDE desktop. They are used as monitoring, rss reader, quick application access dialogs and so on. I use plasmoids for the remoting monitoring of my root server. Plasmoids can be implemented in C/C++, Ruby, Python, Java, et cetera. The following example ( derived from KDE tutorial) describes an easy hello world plasmoid:

(1) First you create the plasmoid project directory and all necessary files:

helloWorld (dir)
- metadata.desktop (file) - contents (dir) - code (dir) - main.py (file)
(2) metadata.desktop:
[Desktop Entry]
Encoding=UTF-8
Name=Hello World Example
Name[tr]=Rafael Sobek
Type=Service
ServiceTypes=Plasma/Applet
X-Plasma-API=python
X-Plasma-MainScript=code/main.py
Icon=applications

X-KDE-PluginInfo-Author=Rafael Sobek
X-KDE-PluginInfo-Email=info@test.org
X-KDE-PluginInfo-Name=helloWorld
X-KDE-PluginInfo-Version=0.1
X-KDE-PluginInfo-Website=http://developers-blog.org
X-KDE-PluginInfo-Depends=
X-KDE-PluginInfo-License=LGPL
(3) main.py:
# 
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript
 
class HelloWorldApplet(plasmascript.Applet):
    def __init__(self,parent,args=None):
        plasmascript.Applet.__init__(self,parent)
 
    def init(self):
        self.resize(200, 200)
        self.setAspectRatioMode(Plasma.IgnoreAspectRatio)
        # No configuration interface supported
        self.setHasConfigurationInterface(False)
 
    def paintInterface(self, painter, option, rect):
        painter.save()
        painter.setPen(Qt.white)
        painter.drawText(rect, Qt.AlignVCenter | Qt.AlignHCenter, "Hello developers-blog.org!")
        painter.restore()
 
def CreateApplet(parent):
    return HelloWorldApplet(parent)

After that you zip the plasmoid project directory (helloWorld.zip) and install the plasmoid with the following command.

plasmapkg -i helloWord
After that you can test your installed plasmoid with:
# without suffix (.zip)
plasmoidviewer helloWorld 

Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 9:33 AM in Uncategorized

 

[Trackback URL for this entry]

Comment: verhoevenv at Fr, 13 Nov 1:48 AM

Quick note, it should be "plasmapkg -i helloWord.zip" instead of "plasmapkg -i helloWord" (i.e. you just give the filename to plasmapkg).

Installed plasmoids can also be found in the "add widget" dialog screen.

Comment: Andey at Mi, 21 Apr 8:55 PM

exactly what i needed, thanks

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« June »
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
282930