© 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 <email@example.com> has adapted the tutorial to version 0.9.
I decided to go with KDE + Qt, for several reasons:
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.