Saving config of a Python plasmoid

Tobias Leupold tobias.leupold at web.de
Sun Dec 1 13:17:00 UTC 2013


Hello list,

I have a question about a plasmoid written in Python. I's quite a basic 
question and surely very simple to solve, but I didn't find a tutorial how to 
do it and I also could not get it out of other Python plasmoids.

How can I save and restore the configuration of a Python plasmoid? I'm pretty 
sure there's a nice high-level KDE/Qt way to do it, isn't it? I managed to 
write a plasmoid with a config dialog. I also managed to access the selected 
config values, but I don't know how to save the config and restore it later.

I would really appreciate help about this. It surely would also be fine if 
some example was included in the Howto found at 
http://techbase.kde.org/Development/Tutorials/Plasma#Python as probably other 
people trying to write a Python plasmoid will also run into this.

Thanks in advance for all help!

Here's a minimal example able to access the selected value from the config 
dialog but lacking the ability so save it that can be run via plasma-windowed:

metadata.desktop:

	[Desktop Entry]
	Encoding=UTF-8
	Name=Config Test
	Type=Service
	ServiceTypes=Plasma/Applet
	X-Plasma-API=python
	X-Plasma-MainScript=code/main.py

contents/code/main.py:

	# -*- coding: utf-8 -*-
	
	from PyQt4 import QtCore
	from PyKDE4.plasma import Plasma
	from PyKDE4 import plasmascript
	
	class configTest(plasmascript.Applet):
	
	        def __init__(self, parent, args = None):
	                plasmascript.Applet.__init__(self, parent)
	
	        def init(self):
	                self.setAspectRatioMode(Plasma.IgnoreAspectRatio)
	
	        def paintInterface(self, painter, option, rect):
	                painter.save()
	                painter.setPen(QtCore.Qt.black)
	                painter.drawText(rect, QtCore.Qt.AlignLeft, 
str(self.config('main').readEntry('testEntry')))
	                painter.restore()
	
	def CreateApplet(parent):
	        return configTest(parent)

contents/ui/config.ui:

	<?xml version="1.0" encoding="UTF-8"?> 
	<ui version="4.0"> 
	 <class>Config</class> 
	  <widget class="QWidget" name="verticalLayoutWidget"> 
	   <layout class="QVBoxLayout" name="verticalLayout"> 
	    <item> 
	     <widget class="KUrlRequester" name="kcfg_testEntry"/> 
	    </item> 
	   </layout> 
	  </widget> 
	 <resources/> 
	 <connections/> 
	</ui>

contents/config/main.xml:

	<?xml version="1.0" encoding="UTF-8"?>
	<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
	      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	      xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
	                          http://www.kde.org/standards/kcfg/1.0/kcfg.xsd">
	  <kcfgfile name="configTestrc"/>
	  <include>kglobalsettings.h</include>
	  <group name="main">
	    <entry name="testEntry" type="Url"></entry>
	  </group>
	</kcfg>



More information about the Plasma-devel mailing list