24.1. Tkinter — Python interface to Tcl/Tk

The Tkinter module (“Tk interface”) is the standard Python interface to the Tk GUI toolkit. Both Tk and Tkinter are available on most Unix platforms, as well as on Windows systems. (Tk itself is not part of Python; it is maintained at ActiveState.)

Note

Tkinter has been renamed to tkinter in Python 3.0. The 2to3 tool will automatically adapt imports when converting your sources to 3.0.

See also

Python Tkinter Resources
The Python Tkinter Topic Guide provides a great deal of information on using Tk from Python and links to other sources of information on Tk.
An Introduction to Tkinter
Fredrik Lundh’s on-line reference material.
Tkinter reference: a GUI for Python
On-line reference material.
Python and Tkinter Programming
The book by John Grayson (ISBN 1-884777-81-3).

24.1.1. Tkinter Modules

Most of the time, the Tkinter module is all you really need, but a number of additional modules are available as well. The Tk interface is located in a binary module named _tkinter. This module contains the low-level interface to Tk, and should never be used directly by application programmers. It is usually a shared library (or DLL), but might in some cases be statically linked with the Python interpreter.

In addition to the Tk interface module, Tkinter includes a number of Python modules. The two most important modules are the Tkinter module itself, and a module called Tkconstants. The former automatically imports the latter, so to use Tkinter, all you need to do is to import one module:

import Tkinter

Or, more often:

from Tkinter import *
class Tkinter.Tk(screenName=None, baseName=None, className='Tk', useTk=1)

The Tk class is instantiated without arguments. This creates a toplevel widget of Tk which usually is the main window of an application. Each instance has its own associated Tcl interpreter.

Changed in version 2.4:

Changed in version 2.4: The useTk parameter was added.

Tkinter.Tcl(screenName=None, baseName=None, className='Tk', useTk=0)

The Tcl() function is a factory function which creates an object much like that created by the Tk class, except that it does not initialize the Tk subsystem. This is most often useful when driving the Tcl interpreter in an environment where one doesn’t want to create extraneous toplevel windows, or where one cannot (such as Unix/Linux systems without an X server). An object created by the Tcl() object can have a Toplevel window created (and the Tk subsystem initialized) by calling its loadtk() method.

New in version 2.4:

New in version 2.4.

Other modules that provide Tk support include:

ScrolledText
Text widget with a vertical scroll bar built in.
tkColorChooser
Dialog to let the user choose a color.
tkCommonDialog
Base class for the dialogs defined in the other modules listed here.
tkFileDialog
Common dialogs to allow the user to specify a file to open or save.
tkFont
Utilities to help work with fonts.
tkMessageBox
Access to standard Tk dialog boxes.
tkSimpleDialog
Basic dialogs and convenience functions.
Tkdnd
Drag-and-drop support for Tkinter. This is experimental and should become deprecated when it is replaced with the Tk DND.
turtle
Turtle graphics in a Tk window.

These have been renamed as well in Python 3.0; they were all made submodules of the new tkinter package.