< Development | Tutorials | Plasma4Revision as of 11:50, 18 January 2009 by Sedwards (talk | contribs) (fixed links)(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff) Contents 1 Abstract 2 Prerequisites 3 The 'main.py' file 4 Conclusion Abstract Using the paintInterface() method to display applets is a powerful, and sometimes even necessary way of working. For example, when you want to display a SVG graphic. But it is not the only method available to create a Plasmoid, however. Plasma comes with a number of widgets "tailored" for use in Plasmoids. In this tutorial we will create a very simple "Hello, world!" Plasmoid using Plasma widgets. Prerequisites Set up your applet directory and create a metadata.desktop file as described in the Getting Started tutorial. The 'main.py' file Here is how our main.py file will look like: Copyright stufffrom 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) Let's take a deeper look at it. As in the main tutorial, we create our class by deriving from the plasmascript.Applet class. In __init__(), we quickly initialize the class. We use the init() method instead to do all the dirty work of creating the applet and adding the widget needed to display the text. First of all we tell Plasma that this applet does not have a configuration dialog (self.setHasConfigurationInterface(False)). We then set the aspect ratio mode to a square (Plasma.Square). This is taken into account when someone resizes the applet. Next, we add a background to the applet using a SVG image provided by the standard Plasma theme. We do so by creating an instance of Plasma.Svg, assigning it to self.theme, and then specifying the SVG graphic we want to use via self.setImagePath(). This way the applet will use the standard "background" SVG provided by the "widgets" directory of our current Plasma theme. We can also use relative paths or absolute paths to SVGs of our choice, should we need to. The setBackgroundHints() method sets the Plasmoid's background to the default Plasma background (Plasma.Applet.DefaultBackground) saving us from having to manually draw the background ourselves. The third and most important step deals with creating a layout. It is a bit different than the standard QLinearLayout seen in Qt and PyQt, because it is done inside a QGraphicsView used by Plasma. We want a simple, linear layout, and so we use a QGraphicsLinearLayout, setting it to be horizontal (Qt.Horizontal). Next we create a Plasma.Label. This is a widget that is Plasma aware and therefore uses all of the goodies provided by Plasma. For example, automatically changing its text color if the Plasma theme changes. We then use the setText() method (analogous to the normal QLabel.setText() method) to set our label text ("Hello, world!") and then add the label to the layout. Finally we resize the applet to 125 x 125 pixels. Once packaged and installed (see the Getting Started tutorial), the Plasmoid will look like this: Conclusion This brief tutorial shows how to use Plasma widgets with your applet. Beside Plasma.Label there are many more widgets that you can use. Notice that at the moment you can only add widgets and create layouts directly in code. This means you can't use an application like Qt Designer to design the appearance of your applet. Retrieved from "https://techbase.kde.org/index.php?title=Development/Tutorials/Plasma4/Python/Using_widgets&oldid=37549" Content is available under Creative Commons License SA 4.0 unless otherwise noted.