As has already been said, a widget is a window object that has state and behavior. In terms of Tcl/Tk a widget is created by calling a widget creation command. There is a specific widget creation for each type of widget.
The widget creation command is supplied with arguments. The first argument is always the name you want to give to the resulting widget; the other arguments set the initial state of the widget.
The immediate result of calling a widget creation command is that it returns the name of the new widget. A side-effect is that the instance of the widget is created and its name is defined as in the Tcl interpreter as a procedure through which the widget state can be accessed and manipulated.
This needs an example. We will use the widget creator command
to make a button widget:
button .fred -text 'Fred' -background red
which creates an instance of a button widget named
.fred that will
display the text
Fred on the button and will have a red background
color. Evaluating this command returns the string
.fred, the name
of the newly created widget.
As a side-effect, a Tcl procedure named
.fred is created.
A call to a widget instance has the following form:
widgetName method methodArgs
where widgetName is the name of the widget to be manipulated, method is the action to be performed on the widget, and methodArgs are the arguments passed to the method that is performed on the widget.
The two standard methods for widgets are
configure - is used to change the state of a widget; for example:
.fred configure -background green -text 'Sid'
will change the background color of the widget
.fred to green
and the text displayed to
cget is used to get part of the state of a widget; for example:
.fred cget -text
Sid if the text on the button
In addition to these general methods, there are special methods
for each widget type. For example, with button widgets
you have the
can be called somewhere in the Tcl code to invoke button
as though it had been clicked on.
can be called somewhere in the Tcl code to cause the button to flash.
We will come across some of these special method when we discuss the widgets in detail. For a comprehensive list of widget methods, refer to entry for the appropriate widget creation command in the Tcl/Tk manual.
We now discuss the widget creation command for each widget type.