Tk Labels

The Tk Label Widget

The label widget displays a read-only text, bitmap or image label. Here I'll concentrate on text and bitmap labels, and defer image labels until later.

The command to create labels is the label command:

label .l -text "A label!" -bg grey55

As usual, the first argument, .l, is the name of the new label to create, and rest are arguments that affect the look and feel of the label. The -bg argument specifies the background color, a shade of grey in this case, and the -text argument determines what to display in the label.

All Tk widgets have a common set of options, in addition to some options that are specific to each widget. For example, the -bg option is accepted by all widgets, while -text only makes sense in a label and in a small number of other widgets.

The text displayed in a label can be specified indirectly, by using the -textvariable option. This option says that the text displayed by the label comes from a global Tcl variable. Additionally, it sets things up so that every time the value of the variable changes, the text displayed in the label also changes. You can use this for some cool effects:

set labeltext 0
label .l -textvariable labeltext -bg grey55

What's going on here? I specified that the label above should display the value of the labeltext variable, starting at zero, and I set up a timer with an after command that increments the value of the text variable every second.

Here's another example. This label shows the current date and time, by using a text variable and a timer, similar to the preceding Tclet.





comments?