An Introduction to Tkinter | ||
Copyright © 1997 by Fredrik Lundh <[email protected]> | ||
Updated 9 Nov 1997 |
< Hello, Again | Tkinter Classes | Widget Configuration >
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.
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.
The Pack, Place, and Grid classes are used as mixins by the widget classes. They provide access to the various geometry managers, also via delegation.
Manager Description 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. 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.
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 below.
Tkinter support 15 core widgets:
Widget Description 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, creating 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 behaviour. 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.
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.
Also note that there's no widget class hierarchy in Tkinter; all widget classes are siblings in the inheritance tree.
< Hello, Again | Tkinter Classes | Widget Configuration >