Tkinter supports 15 core widgets:
- A simple button, used to execute a command or other operation.
- Structured graphics. This widget can be used to draw graphs and plots, create graphics editors, and to implement custom widgets.
- Represents a variable that can have two distinct values. Clicking the button toggles between the values.
- A text entry field.
- 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.
- Displays a text or an image.
- Displays a list of alternatives. The listbox can be configured to get radiobutton or checklist behavior.
- A menu pane. Used to implement pulldown and popup menus.
- A menubutton. Used to implement pulldown menus.
- Display a text. Similar to the label widget, but can automatically wrap text to a given width or aspect ratio.
- 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.
- Allows you to set a numerical value by dragging a “slider”.
- Standard scrollbars for use with canvas, entry, listbox, and text widgets.
- Formatted text display. Allows you to display and edit text with various styles and attributes. Also supports embedded images and windows.
- A container widget displayed as a separate, top-level window.
In Python 2.3 (Tk 8.4), the following widgets were added:
- A variant of the Frame widget that can draw both a border and a title.
- A container widget that organizes child widgets in resizable panes.
- 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.
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.
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.
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.
- 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.
- 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.
- 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.