back next

tkinter Classes

Widget classes

tkinter supports 15 core widgets:

Button
A simple button, used to execute a command or other operation.
Canvas
Structured graphics. This widget can be used to draw graphs and plots, create graphics editors, and to implement custom widgets.
Checkbutton
Represents a variable that can have two distinct values. Clicking the button toggles between the values.
Entry
A text entry field.
Frame
A container widget. The frame can have a border and a background, and is used to group other widgets when creating an application or dialog layout.
Label
Displays a text or an image.
Listbox
Displays a list of alternatives. The listbox can be configured to get radiobutton or checklist behavior.
Menu
A menu pane. Used to implement pulldown and popup menus.
Menubutton
A menubutton. Used to implement pulldown menus.
Message
Display a text. Similar to the label widget, but can automatically wrap text to a given width or aspect ratio.
Radiobutton
Represents one value of a variable that can have one of many values. Clicking the button sets the variable to that value, and clears all other radiobuttons associated with the same variable.
Scale
Allows you to set a numerical value by dragging a “slider”.
Scrollbar
Standard scrollbars for use with canvas, entry, listbox, and text widgets.
Text
Formatted text display. Allows you to display and edit text with various styles and attributes. Also supports embedded images and windows.
Toplevel
A container widget displayed as a separate, top-level window.

In Python 2.3 (Tk 8.4), the following widgets were added:

LabelFrame
A variant of the Frame widget that can draw both a border and a title.
PanedWindow
A container widget that organizes child widgets in resizable panes.
Spinbox
A variant of the Entry widget for selecting values from a range or an ordered set.

Also note that there’s no widget class hierarchy in tkinter; all widget classes are siblings in the inheritance tree.

All these widgets provide the Misc and geometry management methods, the configuration management methods, and additional methods defined by the widget itself. In addition, the Toplevel class also provides the window manager interface. This means that a typical widget class provides some 150 methods.

Mixins

The tkinter module provides classes corresponding to the various widget types in Tk, and a number of mixin and other helper classes (a mixin is a class designed to be combined with other classes using multiple inheritance). When you use tkinter, you should never access the mixin classes directly.

Implementation mixins

The Misc class is used as a mixin by the root window and widget classes. It provides a large number of Tk and window related services, which are thus available for all tkinter core widgets. This is done by delegation; the widget simply forwards the request to the appropriate internal object.

The Wm class is used as a mixin by the root window and Toplevel widget classes. It provides window manager services, also by delegation.

Using delegation like this simplifies your application code: once you have a widget, you can access all parts of tkinter using methods on the widget instance.

Geometry mixins

The Grid, Pack, and Place classes are used as mixins by the widget classes. They provide access to the various geometry managers, also via delegation.

Grid
The grid geometry manager allows you to create table-like layouts, by organizing the widgets in a 2-dimensional grid. To use this geometry manager, use the grid method.
Pack
The pack geometry manager lets you create a layout by “packing” the widgets into a parent widget, by treating them as rectangular blocks placed in a frame. To use this geometry manager for a widget, use the pack method on that widget to set things up.
Place
The place geometry manager lets you explicitly place a widget in a given position. To use this geometry manager, use the place method.

Widget configuration management

The Widget class mixes the Misc class with the geometry mixins, and adds configuration management through the cget and configure methods, as well as through a partial dictionary interface. The latter can be used to set and query individual options, and is explained in further detail in the next chapter.

 

A Django site. rendered by a django application. hosted by webfaction.