[Main Page] [KHello #1] [KHello #2] [KHello #3] [KHello #4] [KHello #5]

Hello World - first try


As Daniel says, this is the simplest possible KDE program, nothing more than a window. It doesn't even say hello.


#!/usr/bin/env python

import sys 
from kdeui import * 

a=KApplication(sys.argv)
w=QWidget()
a.setMainWidget(w)
a.setTopWidget(w)
w.resize(200,100)
w.show()
a.exec_loop()

What can we learn from this? Well, we need to load the sys module, in order to be able to pass the commandline arguments to KApplication. That's something that comes for free with C++.

Since version v0.9 of the Python Bindings for KDE, it is no more sufficient to just import the kde module in order to have access to the complete KDE toolkit.

Importing the whole kde module used to load seven megs of library code, of which four or five megs were shared. This still was an improvement over the twenty-five megs, which had been necessary beforehand.

PyKDE memory usage has become much more frugal than that by splitting the bindings into the following libraries:

As a trade off to reduced memory usage, the programmer now has to find the right libraries to import. In our simple case, it is sufficient to just import kdeui. (BTW: To see what is contained in kdeui, fire up your python interpreter, do an "import kdeui", and a "dir(kdeui)".)

Well, then we create an application. An application is not the same as a window, and doesn't show up on the screen.

To achieve that landmark, we make a widget, of the type QWidget, make it the main widget of our application, and put it on top, resize it, show it and call exec_loop.

Now that last line is worthy of our attention. It starts the main loop of the application, the equivalent of (in pseudo-code)

  do
     if event=="quit" then exit loop
     if event=="doThis" then call doThis
  loop

The c++ equivalent of the python a.exec_loop() is

   return a.exec()

It seems we don't return an exit status from the main loop of the gui. And the exec is renamed to exec_loop because of a naming conflict.

This was easy, wasn't it? Why not try it with the string "Hello World!" as the second argument in a=KApplication(sys.argv), like a=KApplication(sys.argv, "Hello World!").

Oh, and be sure to check your ~/.kde/share/config/ directory after running this example!


Changes