Previous: Widgets
Up: Widgets
Next: Message widgets
Previous Page: Widgets
Next Page: Message widgets

Label widgets

The following ÒprogramÓ creates a text label.

label .label1  -text "This is a text label"
    pack  .label1

The label command causes the label to be generated, while the pack command is needed to cause the label to be displayed. Further information about packing will be given later.

To test this, store it in a file, say label.tcl, and carry out the following on the Brunel computer system:

use tcl-7.3    (This configures the system to 
                    recognise the TCL software)
    wish -f label.tcl

After a few seconds, a small window containing text appears, as shown in figure 1.

An alternative way of running the same program is to issue the command

wish

which will cause a rectangular window to appear on the screen as shown, and to give a % prompt in the current text window. Now type

source label.tcl

and the label widget as shown previously in figure 1 will replace the rectangular window.

Labels are used for attaching text to other widgets, as will be seen later.

This very simple label program can be modified to show other features, including foreground and background colours, and boxes round the text.

For example

label .label2 -text "More text"Ó -relief sunken
     pack  .label2

gives a boxy feel to the label text, while

label .label3 -text "Coloured text" -background red -foreground blue
     pack  .label3

gives coloured text.

The next example shows how a TCL variable can be used to provide the text

label .label4 -text $text
    pack  .label4

When running this example run the wish command first, and start the widget using the source command.

wish
    set text "Hello there"
    source label4.tcl
    set text "Bye for now".

In this example, the label text is constant, and does not change when the text variable is modified.

If the label is specified by the use of the textvariable option, as in the following modified form for label4

label .label4 -width 20 -textvariable text

then changes to the variable text will be reflected in the label. Notice how the label text changes as the second set command is executed when this modified label is used.

The final example of this section shows most of the major configuration features of label widgets:

#This example shows different forms for the label widget
     label .lab1 -text "This is text"
     label .lab2 -text "This is coloured text" -foreground red  
                 -background blue
     label .lab3 -text "Sunken text"  -relief sunken
     label .lab4 -text "Grooved text" -relief groove
     label .lab5 -text "Flat text"    -relief flat
     label .lab6 -text "Ridged text"  -relief ridge
     label .lab7 -text "Raised text"  -relief raised
     label .lab8 -text "Times font"  
                  -font *-times-medium-r-normal--*-100-*
     pack  .lab1 .lab2 .lab3 .lab4 .lab5 .lab6 .lab7 .lab8  
                    -padx 2m -pady 1m -fill x

Note the use of comments introduced by # as the first character on each line, and also the use of as a line continuation character in this example.



Previous: Widgets
Up: Widgets
Next: Message widgets
Previous Page: Widgets
Next Page: Message widgets

csstddm@brunel.ac.uk
Fri Aug 19 16:55:19 BST 1994