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

Hello World - second attempt

Well, people who are doing Daniels tutorial in parallel will notice that he now uses three files. In order to add verisimilitude to an otherwise unrealistic example, I've split the Python hello world application in two files - one with a class definition, and one with the application definition. So, without further ado, although it must be said that it's still singularly uncommunicative, the code:


khello.py

import sys
from kdeui import KTMainWindow
from kdecore import kapp

class khello(KTMainWindow):
  
  def __init__(self):
    KTMainWindow.__init__(self)
    
  def closeEvent(self, QCloseEvent):
    kapp.quit()
    

khello_main.py

#!/usr/bin/env python

import sys        
from kdeui import KApplication 
import khello    

app=KApplication(sys.argv, "Hello World!")
hello=khello.khello()
hello.show()
app.exec_loop()

Well, let's take the class definition first. The C++ class definition looks as follows:


/********************* khello.h *********************/
#include <kapp.h>
#include <ktopwidget.h>

class KHello : public KTopLevelWidget
{
Q_OBJECT;
public:
void closeEvent(QCloseEvent *);
};

/********************* khello.cpp *********************/

#include "khello.moc"

void KHello::closeEvent(QCloseEvent *)
{
kapp->quit();
}

We define a class, Khello, that is a descendent from KTMainWindow, the KDE toplevel widget. Every application that wants to show something on the screen will have a central widget, and this is it. Formerly, it was called KTopLevelWidget, as the c++ source shows, but that name has been obsoleted in favour of KTMainWindow, which could be regarded as more descriptive.

When the widget is created, the constructor __init__ is called first. If we leave out the __init__ constructor in the example above, Python will call the constructor of the base class KTMainWindow on it's own. Here we have made the call to the base class KTMainWindow explicit. In both cases we get a fully initialized toplevel window, so just like C++, Python could happily do without an explicitly given constructor.

When the widget gets thrown a close event, the application quits with kapp.quit(). The function kapp is imported separately from kdecore, and allows access to the single global KApplication object. In our example, this saves us the trouble of having to explicitly pass the global KApplication object from khello_main.py to khello.py.

In comparison to the C++ source source, we will have a lot of occasions to type self, the equivalent of this. We use self, when we refer to the instance of a class. In the __init__ function of class khello, we call the base class KTMainWindow with self as an argument to let the __init__ function of KTMainWindow handle initialisation.

Oh, by the way, Python is very case-sensitive. I had initially written CloseEvent instead of closeEvent, and Python didn't squirm, but neither did it call my closing event.

In our main file we must import our khello.py module, and then create an object of the type khello - that's easy. All we have to do afterwards is show it, and magically the string "Hello World!", which wouldn't have done us any good in example nr. 1, when the widget we showed was a mere QWidget, and not the better, improved KTMainWindow, is shown in the window caption.


Changes