Widgets

The first basic idea behind Tkinter is the concept of a widget, which is short for window gadget. A widget is a data structure that also has a visible, onscreen representation. When the program changes the internal data structure of the widget, that change is automatically displayed on the screen. Various user actions on the visible representation of the widget (mouse clicks and so forth) can, in turn, cause internal changes or actions within the widget's data structure.

Tkinter is a collection of widget definitions, together with commands for operating on them, and a few extra commands that don't apply to any specific widget but that are still relevant to GUI programming. In Python, each different type of widget is represented by a different Python class. A Button widget is of the Button class, a Label widget is of the Label class, and so forth.

This direct mapping between Tkinter widget types and Python classes makes using widgets in a Python program extremely simple. For example, a Python program that creates and uses a Button widget and a Label widget looks something like this:

from tkinter import *

my_button = Button(...optional arguments...) my_label = Label(...optional arguments...)

This style of mapping Python classes to widgets is common in Python GUI environments, although the exact names of the widgets and their parameters will naturally vary.

Was this article helpful?

0 0

Post a comment