PyKDE Programming/Printable version
This is the print version of PyKDE Programming You won't see this message or any elements not part of the book's content when you print or preview this page. |
The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/PyKDE_Programming
Introduction
Requirements
[edit | edit source]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 this
[edit | edit source]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 Application
[edit | edit source]A basic KApplication
[edit | edit source]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 Application
[edit | edit source]A basic QT Application (with a widget)
[edit | edit source]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
DCOP
[edit | edit source]KParts
[edit | edit source]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:
- After importing everything needed, it creates a QApplication (Note that no KApplication is needed for using a KPart).
- A factory is created from the library “katepart”. A factory is a class which generates objects from a library.
- The factory is used to create an instance, a Katepart-object with no parent and the name “PartOfKate”. You can use any name here.
- The widget of the Katepart is resized (It looks better) and shown.
- The main loop of the Application is started.
External Resources
KDE
[edit | edit source]- KDE 4.3 PyKDE API Reference
- General KDE Developping Tutorials in C/C++
- Python Development with KDE
- Common KDE API Reference
Qt
[edit | edit source]