Development/Tutorials/Plasma4/Python/Using widgets

From KDE TechBase
Revision as of 14:46, 17 January 2009 by Einar (talk | contribs) (First draft)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Abstract

Using paintInterface to display applets is powerful, and sometimes even necessary (when you want to display SVG graphics, for example). It is not the only method available to create a Plasmoid, however. Plasma comes with a number of widgets "tailored" for using in Plasmoids. This tutorial will create a very simple "Hello, world!" Plasmoid using this approach.

Prerequisites

Set up your applet directory and create a metadata.desktop file as described in the main tutorial.

The 'main.py' file

Here's how our main.py file will look like:

  1. Copyright stuff

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.setHasConfigurationInterface(False)
       self.setAspectRatioMode(Plasma.Square)
       self.theme = Plasma.Svg(self)
       self.theme.setImagePath("widgets/background")
       self.setBackgroundHints(Plasma.Applet.DefaultBackground)
       self.layout = QGraphicsLinearLayout(Qt.Horizontal, self.applet)
       label = Plasma.Label(self.applet)
       label.setText("Hello world!")
       self.layout.addItem(label)
       self.resize(125,125)

def CreateApplet(parent):

   return HelloWorldApplet(parent)