return to first page linux journal archive
keywordscontents

Swing as a GUI for Python

Swing is the second version of Java's windowing toolkit. It is an interesting beast worth describing a bit here, even though it has almost nothing to do with Python. What strikes me most about Swing is its modern design and impressive size. An example of Swing's modern design is that it is based on the model-delegate pattern (similar to the model-view-controller pattern invented in the Smalltalk community). In this pattern, data structures and graphical representations are loosely coupled, so one can, for example, have two different views of the same data. As evidence of the size of the toolkit, O'Reilly's excellent book on Swing (Java Swing by Eckstein, Loy and Wood) is 1200+ pages, and it's just a detailed tutorial, not a reference manual.

Why mention Swing at all in this article? All of the portable GUI frameworks in Python are layered on top of non-Python systems, be it Tcl/Tk for Tkinter, wxWindows for wxPython, etc. This layering means the Python ports are relatively small and simple to maintain. It also means none of the documentation for the third-party frameworks can be used by programmers who don't know Tcl in the case of Tkinter, or C++ in the case of wxPython, as there is no ``natural'' mapping between those languages--only choices made by the respective authors of the layers. This is where Swing is different.

As mentioned earlier, JPython defines a simple, consistent interface between Python and Java libraries. Most of the type conversions are trivial, and the object models are similar enough that a Python programmer can very quickly learn to read a Javadoc page describing a Java class and know how to call it from Python. This is true for all Java libraries, of which Swing is a large and powerful example. In other words, with a little knowledge which is JPython-general, any Python programmer can read and understand Swing, making Swing probably the most well-documented GUI solution for Python. That said, Swing is undoubtedly the slowest GUI toolkit for Python and has the largest footprint. To summarize, if you have the hardware resources and want to build nice-looking powerful portable GUIs, Swing is worth a look.