display | more...

Tkinter (Tk Interface) is the standard GUI toolkit for Python. It is a straightforward binding to the Tk toolkit, directly mapping Tk widgets to Python objects. It produces functional-looking programs under X11, Windows, and Macintosh, among others. Unlike its Perl counterpart, Perl/Tk, Tkinter is implemented by direct use of the Tcl interpreter rather than by abstracting the UI code from the Tk system. As such, it requires a working Tcl/Tk system.

Using Tkinter

Tkinter is implemented in a module called, strangely enough, 'Tkinter'. Thus, to use the Tkinter library in your Python program you must include the line from Tkinter import *. Communication with Tk is done with an object of the class 'Tk', which is automatically created when any Tkinter object is created.

Tkinter widgets, as with the widgets in all object oriented GUI toolkits, are implemented as class instances. The classes in Tkinter correspond directly to Tk widget types, but unlike basic Tk widgets they can be subclassed to gain non-standard behavior. The Tk properties of each widget are accessible through the dictionary-like syntax myButton["fg"] = 'red'. Actions on the Tk widgets, including packing, are methods of the Tkinter objects, such as myButton.pack()

A Python/Tkinter Hello World program equivalent to the one at Perl/Tk is found below:

#!/usr/bin/env python

from Tkinter import *
main = Frame()
label = Label(main)
label['text'] = 'Hello World!'
button = Button(main)
button['text'] = 'Quit'
button['command'] = main.quit

A more object-oriented Hello World can be found at http://python.org/doc/2.3/lib/node633.html .

Advantages and Disadvantages

Tkinter has two considerable advantages over other Python GUIs. The first advantage is that, as the 'standard' toolkit it is included in the base Python distribution and as such Tkinter code should run on any system with Python installed. The other main advantage is that, since the Tk toolkit has been around for a very long time (since 1991), it is more portable than any other GUI toolkit. Many of the alternatives available don't even support all of X11, Windows, and Macintosh, let alone other, more obscure window systems.

Tkinter also has some disadvantages compared to other GUI toolkits for Python. Compared to wxPython, PyQt, or PyGTK, it has fewer built-in widgets. Some additional widgets are provided by the Tix extension, but there are still more widgets available in other toolkits, including HTML windows. The layout abilities of Tk, and thus Tkinter, are rather simplistic in places, compared with the sophisticated layout options of other toolkits. A somewhat shallower criticism of Tkinter is that, on platforms like X11 where it doesn't bind to native widgets, it is quite ugly and does not integrate well with the desktop environment. In contrast, wxPython, PyQT, and PyGTK all bind to modern toolkits, with PyQT and PyGTK having PyKDE and PyGnome extensions respectively to completely fit into the respective desktop environments.

It is said to be an annual tradition among the Python hackers to debate ditching Tkinter... but it persists.

This writeup is copyright 2001-2004 D.G. Roberge and is released under the Creative Commons Attribution-NonCommercial-ShareAlike licence. Details can be found at http://creativecommons.org/licenses/by-nc-sa/2.0/ .

Log in or register to write something here or to contact authors.