PyKDE Programming/Printable version


PyKDE Programming

The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/PyKDE_Programming

Permission is granted to copy, distribute, and/or modify this document under the terms of the Creative Commons Attribution-ShareAlike 3.0 License.

Introduction

RequirementsEdit

This book requires a basic understanding of Python and Linux. In order to follow the material taught the following programs and libraries should be installed:

  • Python2.6 (since PyKDE4 is not ported yet to Python3)
  • PyKDE4
  • PyQt4

How to read thisEdit

This book is organized in three major parts

  • Building KDE Applications
  • Creating Qt based GUIs
  • Bringing the two concepts together


KDE

The Structure of a KDE ApplicationEdit

A basic KApplicationEdit

Every KDE application you are going to write from now on will contain most of the following code.

#!/usr/bin/python
import sys
from PyKDE4.kdecore import ki18n, KCmdLineArgs, KAboutData
from PyKDE4.kdeui   import KApplication

appName     = "kde-application"
catalog     = "simple test"
programName = ki18n("KDE Application")
version     = "1.0"

aboutData = KAboutData(appName, catalog, programName, version)
 
KCmdLineArgs.init (sys.argv, aboutData)
 
app = KApplication ()
 
# your main widget (the application window) would be created and shown here
 
sys.exit(app.exec_())

Go ahead, run this file. The program will load and will not end until you terminate it.

Next we will learn how to add a first widget.


QT

The structure of a QT ApplicationEdit

A basic QT Application (with a widget)Edit

You will create your first Window in this application.

#!/usr/bin/env python
import sys
from PyQt4.QtGui  import *
from PyQt4.QtCore import *

app = QApplication(sys.argv)

win = QWidget()
win.show()

sys.exit(app.exec_())

Did you notice the differences? You did less and got more. But don’t let this fool you. KDE requires this information, but from now on, i doesn’t get any more complicated in KDE than in pure Qt.

Maybe you did spot what we did now and didn’t do earlier: We constructed a widget and showed it. Clicking on the standard “x”-button it closes the whole application by default, but you can change that, of course. You can now transfer the widget-code into the KDE application but you wouldn’t want to write KDE applications if you didn’t want to use KDE’s features, do you?

Let’s move on, but without wasting space: I will leave out everything redundant now, you should keep it in your script, though. Everything we care about now is what we can do between these two lines:

win = QWidget()
win.show()

What about using our window as a frame for other widgets?

In order to do that, we will need a so-called layout and add our widgets to it:

win = QWidget()

layout = QVBoxLayout()
layout.addWidget(QLabel("This is a Label showing text,<br> but it can contain a Picture instead"))
layout.addWidget(QPushButton("Push me"))

win.setLayout(layout)
win.show()

What have we done? Quite something, I guess. We have:

  • Created and shown our widget, as we did before
  • Created a layout which stacks widgets vertically (VBox) and told the widget to use it
  • Added two more widgets to it, both containing text. (As you can see, the QLabel even supports HTML-stuff like the <br>-linebreak)


KDE Extras

DCOPEdit

KPartsEdit

A mimimal example of an application using the Kate KPart (Katepart):

import sys
from PyKDE4.kdecore import KLibLoader
from PyKDE4.kparts import KParts
from PyQt4.QtGui import QApplication

app = QApplication(sys.argv)
factory = KLibLoader.self().factory("katepart")
part = factory.create(None, "PartOfKate")
w = part.widget()
w.resize(500,300)
w.show()
sys.exit(app.exec_())

The script does the following:

  1. After importing everything needed, it creates a QApplication (Note that no KApplication is needed for using a KPart).
  2. A factory is created from the library “katepart”. A factory is a class which generates objects from a library.
  3. The factory is used to create an instance, a Katepart-object with no parent and the name “PartOfKate”. You can use any name here.
  4. The widget of the Katepart is resized (It looks better) and shown.
  5. The main loop of the Application is started.


External Resources

KDEEdit

QtEdit