1. Tkinter widgets
Tkinter provides a collection of widgets that are used to build graphical user interfaces (GUIs). Widgets are the visible components of an application, such as buttons, labels, text boxes, menus, and list controls. Each widget is designed for a specific purpose and can be customised using configuration options such as colours, fonts, sizes, and event bindings.
Most widgets are created by passing a parent widget (typically a Tk window or Frame) followed by optional keyword arguments.
- widget = tk.Widget(parent, option=value)
parentis the window or frame or container that will contain the widget.Widget options are supplied as keyword arguments separated by commas.e.g.button = tk.Button(root, text="OK")
Widgets are typically positioned within their parent using one of Tkinter's geometry managers:
pack()- Arranges widgets one after another.grid()- Arranges widgets in rows and columns.place()- Positions widgets using absolute or relative coordinates.
Note
A widget should generally use only one geometry manager within the same parent container. Mixing pack() and grid() inside the same parent widget can produce unexpected results.
1.1. Common Tkinter widgets
The following table lists the widgets available in the standard tkinter package.
Widget |
Description |
|---|---|
|
A clickable button used to perform an action. |
|
A drawing surface for graphics, images, shapes and custom widgets. |
|
A check box that can be either selected or cleared. |
|
A single-line text entry field. |
|
A container used to group and organise other widgets. |
|
Displays text, images or other information. |
|
A frame with a title displayed along its border. |
|
Displays a list of selectable text items. |
|
Creates application menus and context (popup) menus. |
|
A button that displays an associated menu. |
|
Displays automatically wrapped multi-line text. |
|
Displays a drop-down list of choices. |
|
A container with resizable panes separated by draggable dividers. |
|
Allows one option to be selected from a group. |
|
A slider used to select a numeric value. |
|
Provides horizontal or vertical scrolling for compatible widgets. |
|
Allows selection of a value by typing or using increment/decrement arrows. |
|
A multi-line text editing widget with rich formatting support. |
|
Creates an additional application window. |
1.2. Themed ttk widgets
The tkinter.ttk module provides themed versions of many standard Tkinter widgets. These widgets adopt the native appearance of the operating system and are generally recommended for modern applications.
Common ttk widgets include:
ButtonCheckbuttonComboboxEntryFrameLabelLabelFrameMenubuttonNotebookPanedWindowProgressbarRadiobuttonScaleScrollbarSeparatorSizegripSpinboxTreeview
Note
Some widgets, such as Canvas, Text, Listbox and Menu, do not have themed ttk equivalents and continue to use their standard tkinter implementations.
1.3. Choosing the right widget
Task |
Recommended widget |
|---|---|
Display text |
|
Display long text |
|
Single-line input |
|
Multi-line input |
|
Choose one option |
|
Choose many options |
|
Choose from a list |
|
Numeric input |
|
Perform an action |
|
Group widgets |
|
Display graphics |
|
Show hierarchical data |
|