Paths/Developer/Creating plasmoids with scripts

< Paths‎ | Developer
Revision as of 23:59, 30 December 2008 by Tampakrap (Talk | contribs) (Python scriptengine tutorial)

Jump to: navigation, search

Plasma Chani's advantures in PlasmaLand:

Tutorial 1:

TOC: Layout Hello World Applet Installation via Plasmapkg

Python hello world.


 Each scripted plasmoid shares the same layout. The base folder name is, your applet's name, like "hello world"



The base folder contains a metadata.desktop, which should look a little something like:

[Desktop Entry] Encoding=UTF-8 Name=Hello World Type=Service ServiceTypes=Plasma/Applet X-Plasma-API=python

X-KDE-PluginInfo-Author=Matthew Adams X-KDE-PluginInfo-Email= X-KDE-PluginInfo-Name=myHelloWorld X-KDE-PluginInfo-Version=1.0 X-KDE-PluginInfo-Website= X-KDE-PluginInfo-Category=Examples X-KDE-PluginInfo-Depends= X-KDE-PluginInfo-License=GPL X-KDE-PluginInfo-EnabledByDefault=true



In Base/contents/code you will have your main script file, named "" This is your main script file. The should have a function called "def CreateApplet(parent):" that returns an object that inherits plasma.Applet. in order to be able to inherit plasma.Applet you will need to include a couple specific headers. Here's the helloworld script:

(I haven't tested this script, just hacked up an already existing one on the fly, it might have typos, and as a note, there is a huge difference between plasma.Applet and Plasma.Applet.)

  1. Import all the headers for Qt and KDE

from PyQt4.QtCore import * from PyQt4.QtGui import * from PyKDE4.kdecore import * from PyKDE4.kdeui import * from PyKDE4.plasma import Plasma import plasma

class HelloWorld(plasma.Applet):

 def __init__(self, parent, args=None):
   plasma.Applet.__init__(self, parent)
 def init(self):
   self.text = "Hello World"
   self.svg =
   self.svg.imagePath = 'widgets/background'
 def paintInterface(self):
    p.pen = Plasma.Theme.defaultTheme.color(Plasma.Theme.TextColor)
    plainFont = KDE.GlobalSettings.generalFont
    plainFont.pointSizeF = [rect.height, KDE::GlobalSettings.smallestReadableFont.pointSize].max
    preparePainter(p, rect,@plainFont, self.text)
    p.drawText(, self.text, QTextOption(QAlignCenter))
    self.svg.paint(p, QRectF(rect), "widgets/background")
  def preparePainter(p, rect, font, text)
        tmpRect = QRect
        tmpFont = font
        # Starting with the given font, decrease its size until it'll fit in the
        # given rect allowing wrapping where possible


          p.font = tmpFont
          tmpFont.pointSize = [KDE/GlobolSettings.smallestReadableFont.pointSize, tmpFont.pointSize - 1].max
          tmpRect = p.boundingRect(rect, QTextWordWrap, text)
        while tmpFont.pointSize > KDE::GlobalSettings.smallestReadableFont.pointSize && (tmpRect.width > rect.width || tmpRect.height > rect.height)
           return tmpRect

def CreateApplet(parent):

 return HelloWorld(self)


The SVG, The Font, and the Applet have to be made. The SVG is basically the Plasma Theme system. The font is resized to fit inside the bounding rect of the applet (Which can change when the user resizes it), Then the text is drawn to the screen using the resized font. Fairly simple.

CreateApplet gets called at startup, and returns the applet we made. It's paintinterface button is called every so often to have the interface painted (text draw to the screen), first __init__() get's called, but then the library calls "init(self)" on the object, which is where all the real setup happens.


 Once you have it all setup, simply run:
 cd /path/to/BASE/
 zip -r ../HelloWorld.plasmoid *
 plasmapkg -i ../HelloWorld.plasmoid


 On old kde 4.1.X plasmapkg didn't work with plasmapkg -r ../HelloWorld.plasmoid
 It does in 4.2X - use plasmapkg -r ../HelloWorld.plasmoid in 4.2X
 In 4.1.X do rm -r ~/.kde/share/apps/plasma/plasmoids/HelloWorld to remove

Note: Once you complete a useful or fun plasmoids, you can put it on under the scripted plasmoids section, and have it show up in Get Hot New Stuff on peoples computers without them ever opening a web browser.