Tutorial - First Program

Hello, World

This is the first tutorial for PyKDE4.

PyKDE is a set of bindings for KDE's kdelibs. That means it provides programming access to KDE's basic classes and methods directly from the Python programming language.

This tutorial does a thorough dissection of a simple program - the traditional "Hello, World" program. You can see the complete source code for this program (and other tutorial programs and example programs) in the Source on pykdedocs. You can also run the program just by clicking a button in the Sample tab of pykdedocs (or you can run the program from the command line as well).

Basics

The first section of most Python programs is the import section, where the programmer specifies the Python modules the program will use


import sys

from PyQt4.QtCore import QString

from PyKDE4.kdecore import ki18n, KAboutData, KCmdLineArgs
from PyKDE4.kdeui import KApplication, KGuiItem, KMessageBox

In our import section, we import the sys mdoule to get sys.argv for the command line arguments. We also need one class from PyQt (QString) and a method (ki18n) and a number of classes from PyKDE's kdecore and kdeui modules.

The next section is fairly typical code for the startup of most PyKDE programs (KDE programs written in C++ follow similar steps).

First, we define some variables:

file:///home/jim/pykdedocs/tutorials/started/helloworld.html
appName     = "helloworld"
catalog     = ""
programName = ki18n ("helloworld")
version     = "1.0"
description = ki18n ("Tutorial - First Program")
license     = KAboutData.License_GPL
copyright   = ki18n ("(c) 2007 Jim Bublitz")
text        = ki18n ("none")
homePage    = "www.riverbank.com"
bugEmail    = "jbublitz@nwinternet.com"

The k18n calls used to wrap some of the character strings are part of KDE's localization (translation) features. In this case they're required, because we want to use the variables in creating a KAboutData object:


aboutData   = KAboutData (appName, catalog, programName, version, description,
                            license, copyright, text, homePage, bugEmail)

KAboutData is the class that fills in the data in the About box you can pop up from the Help menu.

In the KAboutData constructor (the method call that creates the KAboutData object), the arguments programName, description, copyright, and text are all of type KLocalizedString (see the KAboutData class reference page - select kdecore - global - KAboutData from the tree on the left side of pykdedocs).

About the only way to create an object of KLocalizedString type is to use the ki18n function.

Nearly every PyKDE program also require and call to KCmdLineArgs.init, or something effecitively the same. In order to create a KApplication object to run our program, we need to call this method with the command line arguments entered when the program was started (sys.argv) and a valid KAboutData object.

KCmdLineArgs.init (sys.argv, aboutData)

Since we want this to be a KDE-based application and have access to KDE's other components, we have to create a KApplication object. Programs without a KApplication object won't be able to access much of PyKDE, since most of KDE's components depend on the existence of this object.

app = KApplication ()

Next, we create a KGuiItem to handle some basic information - some text ("Hello"), no icon (the empty QString () constructor), a tooltip and a "what's this?" entry.


guiItem = KGuiItem (QString( "Hello"), QString(),
                    QString( "this is a tooltip" ),
                    QString( "this is a whatsthis" ))

Finally, we pop up a KMessageBox dialog to display our message.

KMessageBox.questionYesNo (None, "Hello World", "Hello", guiItem )

Final Notes

You may have noted that we didn't explicitly create a KMessageBox object to create the message box. That's because most of the KMessageBox methods are static - we can call them directly without creating an object. (The KCmdLineArgs.init call is also a static method - we didn't create a KCmdLineArgs object before using KCmdLineArgs.init.

There are also two things missing from this program that most PyKDE applications have - a KMainWindow and a call to KApplication.exec_. Those are covered in the next Tutorial.

Acknowledgement

This tutorial is based on the original KDE "Hello, World" tutorial found on Techbase.