s Python + KDE Tutorial

GUI Programming with
      Python and Qt

Quick Links

Python index
Database Objects
Kura (now maintained by Peter Bouda)
CGI scripting and MySQL
Python + KDE Tutorial
Python + KDE Resources
PyKDE example programs
Python and Qt2
PyKDE and PyQt news
A population simulation with Stackless Python
Drift: a language simulation in Python
Anonymous CVS access

Tutorial Links

Lesson 1: KHello
Lesson 2: KDraw
Lesson 3: Edit

The whole tutorial in one file.


  • 26-03-2000: Redesign.
  • 02-11-1999: Updated to version 0.9 (Maik Röder <roeder@berg.net>)
  • 09-06-1999: Updated to version 0.8
  • 09-05-1999: Added link to zip of whole tutorial
  • 17-04-1999: Added Edit
  • 17-04-1999: Adapted to version 0.7
  • 13-04-1999: Creation

© 1999 Boudewijn Rempt


Python + KDE Tutorial


The material in this tutorial is obsolete: I only keep it here for sentimental reasons. I have written a book on PyQt; please go and read that!

A few weeks ago I found myself suddenly in need of a few Linux applications I could be sure no-one else would want to write for me. So I had to do it myself. Now, I do know a few programming languages, ranging from Pascal via PL/SQL to Visual Basic and a few others, too, but what I didn't have was much spare time, so what I needed was a language that would enable me to write working applications with even less code than Visual Basic needs. C and C++, and Pascal too, are just too time-consuming in the matter of string manipulation, and Visual Basic doesn't run under Linux, and it's far too bug-ridden, anyway.

The language I needed was Python, of course. Easy to read, easy to write, full of the most powerful language constructions known to computerkind. Yes, I immediately became an enthousiast. After a few applications with CGI-interfaces, I needed a gui, though. Web interfaces are quite a bit of all-right, but not so hot for more complex applications.

So I started evaluating GUI's for Python, of which there is a bewildering variety. To wit: tkInter, Wpy, pyKDE, KDEBindings, pyGTK, wxWindows, and Guido knows how many more. First I tried tkInter, but found it a bit too complex for my liking, and tkInter doesn't have a table widget, which I absolutely need. I couldn't get the GTK bindings to work reliably. For KDE and its underlying Qt widget set, there were two bindings. An older, unmaintained set - which is still present on python.org, and a newer, more complete and up-to-date, and more importantly, still maintained set, by Phil Thompson, available from the authors website.

This is now in version 0.11 (the author numbers 0.9, 0.10, 0.11, instead of rolling over to 1.0 after 0.9), and it is a whole lot faster and leaner, if slightly incompatible with the first versions. For instance, the bindings have been split over several libraries: Qt, kdecore, kdeui, kfile, kfm, htmlw, and kspell, making individual applications much more light-weight. Maik Röder <roeder@berg.net> has adapted the tutorial to version 0.9.

I decided to go with KDE + Qt, for several reasons:

  • Qt is a rich, flexible and good-looking widget set
  • Qt has great documentation
  • Qt is stable, easy to install and maintained by a great set of people.
  • KDE is a working desktop environment that offers a lot of functionality.
  • The framework KDE offers is flexible, easy to understand, and since it is based on C++ object-oriented in nature, which fits in beautifully with Pythons pervasive object-orientedness.

Only, there isn't much in the way of documentation for the KDE+Python combo. A few example programs in the distribution, and a file with notes. So, what I decided to do was, take one of the C++ KDE tutorials, and translate them. I took Daniel Marjamäki's tutorial , which is nicely paced and graded, and started to work. All the kudos for the examples and the ideas used therein are for him, all the mistakes are mine, of course.

A week or two later, I had Python versions of the lessons Daniel taught me. Mere days later I could add a third lesson. The tutorial is now comprised of three parts: the first is the obligatory Hello World exercise, the second a small scribbling program, kdraw that was quite a hit with my children. The third is about the building of a small, but functional editor, based on Richard J. Moore's C++ tutorial, which can also be found on the KDE website. Not that I'm all that confident of the quality of my code - any corrections and improvements are gratefully received.

A practical note might be useful for novices like myself. I do all my editing in Nedit, which has sane keybindings (after some serious configuration work with regards to the delete key), and a good Python syntax highlighting mode. Others speak highly of Emacs in combination with its Python mode, but Emacs is not something I have any inclination for. All my indenting is with two spaces, no tabs. I've always considered tabs an unnecessary evil, in any programming environment.