KDE4
Everyone has already managed to go through the dampness of technology, appearance, interface concepts, but real apologists know that when KDE4 enters a stable phase, everything will be fine, and weak spirit renegades who you see could not tolerate a year of constant glitches and return.
Now seriously - the key technology of the new KDE - Plasma. So that in KDE it was good to live, it is necessary to create
plasmoids . Thanks to C and C ++ for the work of the kernel, X server and KDE, and we, the generation of web developers, and generally people accustomed to simplicity, can easily inhabit the system using more familiar languages, for example Python, which, as you know, comes with batteries.
')
Package
For all of the above, you need python plasmoid support by your KDE version. The latest release of Ubuntu has this support.
Python plasmoids are organized in packages. A package is a file structure wrapped in a zip:
./contents
./contents/code
./contents/code/main.py
./metadata.desktop
So, let's begin. Create a folder plasma-informer, and in it the folders and files from the listing. The first in line metadata.desktop, we will assume that the fields are sufficiently speaking:
[Desktop Entry]
Encoding=UTF-8
Name=Informer about something
Type=Service
ServiceTypes=Plasma/Applet
X-Plasma-API=python
X-Plasma-MainScript=code/main.py
Icon=chronometer
X-KDE-PluginInfo-Author=
X-KDE-PluginInfo-Email=deeppy@kuku.ya
X-KDE-PluginInfo-Name=plasma-informer
X-KDE-PluginInfo-Version=1.0
X-KDE-PluginInfo-Website=http://habrahabr.ru
X-KDE-PluginInfo-Category=Tests
X-KDE-PluginInfo-Depends=
X-KDE-PluginInfo-License=GPL
X-KDE-PluginInfo-EnabledByDefault=true
The package description is of course an important part for good integration of the package into all sorts of lists, but the salt of the earth lies in contents / code / main.py, which is supplied with a mass of comments:
#! /usr/bin/python # -*- coding: utf8 -*- from PyQt4.QtCore import * from PyQt4.QtGui import * from PyKDE4.kdecore import * from PyKDE4.kdeui import * from PyKDE4.plasma import Plasma import plasma class MyLabel(Plasma.Label): " , ." def setText(self,text): " . ." width = QFontMetrics(self.font()).width(text) self.setMinimumWidth(width) self.setMaximumWidth(width) Plasma.Label.setText(self,text) class PyInfApplet(plasma.Applet): " . - , ..." def __init__(self,parent,args=None): " , ." plasma.Applet.__init__(self,parent) # , ! def init(self): # self.setHasConfigurationInterface(False) # , self.setAspectRatioMode(Plasma.IgnoreAspectRatio) # , , self.timer = self.startTimer(1000) # "", , self.mlayout = QGraphicsLinearLayout(Qt.Horizontal) # self.label = MyLabel() self.label.setText('Yahooho') # self.mlayout.addItem(self.label) # , Plasma, ! self.setLayout(self.mlayout) def timerEvent(self,ev): " " pass def constraintsEvent(self, constraints): if constraints & Plasma.SizeConstraint: self.resize(self.size()) def CreateApplet(parent): " Plasma" return PyInfApplet(parent)
As you can see from the code, the applet does nothing, but simply occupies the panel with a silly inscription. But for experiments with a plasmoid, it is time to install it and see how it works. At the root of the package, run zip -r ../plasma-informer.zip *, and then install it: plasmapkg -i ../plasma-informer.zip
The package will be installed in the home directory in the folder .kde / share / apps / plasma / plasmoids / plasma-informer, and you can continue the development work by editing the file .kde / share / apps / plasma / plasmoids / plasma-informer / contents / code / main.py. The second is to look at the result, and all Pythonists know the property of the packages - if you have already imported a package, then in order to update its code, it is advisable to reload the entire python code. In our case, this is equal to the overload of Plasma, which is generally a risky operation - you will remain with a bare desktop without controls. So, for the Jedi there is a special utility - plasmoidviewer.
~ $ plasmoidviewer plasma-informer

Since the plasmoid is already installed, you can look at it: plasmoidviewer plasma-informer
To load our informer with meaning, edit the timerEvent code:
def timerEvent(self,ev): " " self.label.setText(open('/proc/loadavg','r').read().strip())
Now the informer really informs us about something like that.

But with the placement of the second applet (one more you can see in the picture, it serves to control Amarok) problems arose during import. A small workaround is to edit /usr/share/kde4/apps/plasma_scriptengine_python/plasma_importer.py - in the PlasmaImporter class, I rendered the declaration self.toplevel = {} from __init__ and did this:
class PlasmaImporter(object): toplevel = {} def __init__(self): sys.path.append('<plasma>') sys.path_hooks.append(self.hook)
Such a chip makes toplevel belonging to a
class and not to an object instance, as it was at the very beginning it put me in a stupor for two hours, but it helped here.
Homework - monitoring of own karma, the code for check is accepted in the comment.
PS cat plasma-restart
kbuildsycoca4
kquitapp plasma
plasma
PPS I put KDE4.2, got out a bug, more precisely, the modules were slightly changed. Replace the import plasma line with from PyKDE4 import plasmascript as plasma, and everything will work again.
PPPS After a series of updates, I don’t remember from what moment it broke again. Well, the solution is to add a line to metadata.desktop: X-Plasma-MainScript = code / main.py