[Kde-bindings] Python plasmoid

Gokmen GOKSEL gokmen at pardus.org.tr
Tue Dec 16 07:28:46 UTC 2008


On Tuesday 16 December 2008 01:03:43 Jeremy Sanders wrote:
Hi,

> metadata.desktop:
> [Desktop Entry]
> Encoding=UTF-8
> Name=Python Test
> Type=Service
> ServiceTypes=Plasma/Applet
> X-Plasma-API=python
> Icon=chronometer
>
> X-KDE-PluginInfo-Author=Jeremy Sanders
> X-KDE-PluginInfo-Email=jeremy at jeremysanders.net
> X-KDE-PluginInfo-Name=pytest
> X-KDE-PluginInfo-Version=1.0
> X-KDE-PluginInfo-License=GPL
> X-KDE-PluginInfo-EnabledByDefault=true
Looks fine :)

> In contents/code/main.py:
>
> #!/usr/bin/python
>
> import PyQt4.Qt as qt
> import plasma
Is there a module named plasma ?

You need to import plasmascript from PyKDE4 if you dont have you can check-out 
from [1]

> from PyKDE4.plasma import Plasma
> from PyKDE4.kdecore import KGlobal
You don't need KGlobal for now

> class PyTestApplet(plasma.Applet):
>      def __init__(self, parent, args=None):
>          plasma.Applet.__init__(self,parent)
And you need to use plasmascript.Applet not plasma.Applet

>      def init(self):
>          self.setHasConfigurationInterface(False)
>          self.resize(125, 125)
>          self.setAspectRatioMode(Plasma.Square)
These ok

>      def shape(self):
>          path = QPainterPath()
>          path.addEllipse(self.boundingRect().adjusted(-2, -2, 2, 2))
>          return path
You don't need shape.. For now.

>      def paintInterface(self, painter, option, rect):
>          painter.drawText(rect, "Hi there")
You should set a pen for your painter like;
 painter.setPen(Plasma.Theme.defaultTheme().color(Plasma.Theme.TextColor))

and before painting you need store current painter state with;
 painter.save()

and after drawing text with;
 painter.drawText(rect, Qt.AlignCenter, "Hi there")

you need to restore your painter state with;
 painter.restore()
 
> def CreateApplet(parent):
>      return PyTestApplet(parent)
And it looks ok.

Final result should be [2]

[1] KDE/kdebase/workspace/plasma/scriptengines/python/plasmascript.py
[2] http://cekirdek.pardus.org.tr/~gokmen/cache/main.py

-- 
Gokmen GOKSEL
{ [pardus.org.tr/eng] | [kde.org] } for freedom



More information about the Kde-bindings mailing list