Python Programming/Tkinter

Tkinter is a Python wrapper for Tcl/Tk providing a cross-platform GUI toolkit. On Windows, it comes bundled with Python; on other operating systems, it can be installed. The set of available widgets is smaller than in some other toolkits, but since Tkinter widgets are extensible, many of the missing compound widgets can be created using the extensibility, such as combo box and scrolling pane.

IDLE, Python's Integrated Development and Learning Environment, is written using Tkinter and is often distributed with Python. You can learn about features of Tkinter by playing around with menus and dialogs of IDLE. For instance, Options > Configure IDLE... dialog shows a broad variety of GUI elements including tabbed interface. You can learn about programming using Tkinter by studying IDLE source code, which, on Windows, is available e.g. in C:\Program Files\Python27\Lib\idlelib.

Python 3: The examples on this page are for Python 2. In Python 3, what was previously module Tkinter is tkinter, what was tkMessageBox is messagebox, etc.

Minimal example
A minimal example:

A minimal example made more compact - later references to GUI items not required:

A minimal example creating an application class derived from Frame:

Message boxes
Simple message boxes can be created using tkMessageBox as follows:

Links:
 * The tkMessageBox dialogs module, infohost.nmt.edu
 * Standard Dialogs, effbot.org

File dialog
File dialogs can be created as follows:

Links:
 * The tkFileDialog module, infohost.nmt.edu
 * File Dialogs, effbot.org
 * tkFileDialog, tkinter.unpythonic.net

Radio button
A radio button can be used to create a simple choice dialog with multiple options:

An alternative to radio button that immediately reacts to button press:

Links:
 * The Radiobutton widget, infohost.nmt.edu
 * The Tkinter Radiobutton Widget, effbot.org

List box
A list box can be used to create a simple multiple-choice dialog:

Links:
 * The Listbox widget, infohost.nmt.edu
 * The Tkinter Listbox Widget, effbot.org
 * TKinter Listbox example (Python recipe), code.activestate.com

Checkbox
Checkbox or check button can be created as follows:

Links:
 * The Checkbutton widget, infohost.nmt.edu
 * The Tkinter Checkbutton Widget, effbot.org

Entry
Entry widget, a single-line text input field, can be used as follows:

Links:
 * The Entry widget, infohost.nmt.edu
 * The Tkinter Entry Widget, effbot.org

Menu
Menus can be created as follows:

Links:
 * The Menu widget, infohost.nmt.edu
 * The Tkinter Menu Widget, effbot.org

LabelFrame
A frame around other elements can be created using LabelFrame widget as follows:

Links:
 * The LabelFrame widget, infohost.nmt.edu
 * Tkinter LabelFrame Widget, effbot.org

Message
Message is like Label but ready to wrap across multiple lines. An example:

Links:
 * The Message widget, infohost.nmt.edu
 * The Tkinter Message Widget, effbot.org

Option menu
Drop-down list, in Tkinter option menu, can be created as follows:

Links:
 * The OptionMenu widget, infohost.nmt.edu
 * The Tkinter OptionMenu Widget, effbot.org

Text
Text widget is a more complex one, allowing editing of both plain and formatted text, including multiple fonts.

Example to be added.

Links:
 * The Text widget, infohost.nmt.edu
 * The Tkinter Text Widget, effbot.org

Tcl/Tk version
The Windows installer for Python 2.3 ships with Tcl/Tk 8.4.3. You can find out about the version:

Links:
 * What's new in Python 2.3, python.org

Related books

 * Tcl Programming/Tk