| [ Team LiB ] |
|
The Label WidgetThe label widget provides a read-only text label, and it has attributes that let you control the position of the label within the display space. Most commonly, however, you just need to specify the text for the label: label .version -text "MyApp v1.0" The text can be specified indirectly by using a Tcl variable to hold the text. In this case the label is updated whenever the value of the Tcl variable changes. The variable is used from the global scope, even if there happens to be a local variable by the same name when you create the widget inside a procedure: set version "MyApp v1.0" label .version -textvariable version Example 32-5 A label that displays different strings
proc FixedWidthLabel { name values } {
# name is a widget name to be created
# values is a list of strings
set maxWidth 0
foreach value $values {
if {[string length $value] > $maxWidth} {
set maxWidth [string length $value]
}
}
# Use -anchor w to left-justify short strings
label $name -width $maxWidth -anchor w \
-text [lindex $values 0]
return $name
}
The FixedWidthLabel example is used to create a label with a width big enough to hold a set of different strings. It uses the -anchor w attribute to left-justify strings that are shorter than the maximum. You can change the text for the label later by using the configure widget operation, which can be abbreviated to config:
FixedWidthLabel .status {OK Busy Error}
.status config -text Busy
A label can display a bitmap or image instead of a text string, which is described in Chapter 41 and the section on Bitmaps and Images. This example could use the font metrics facilities of Tk 8.0 to get more accurate sizes of the text for different strings. It is possible, for example, that a three-character string like OOO is wider than a four-character string like llll in a variable-width font. The font metrics command is described on page 640. Label Width and Wrap LengthWhen a label is displaying text, its width attribute is interpreted as a number of characters. The label is made wide enough to hold this number of averaged width characters in the label's font. However, if the label is holding a bitmap or an image, then the width is in pixels or another screen unit. The wrapLength attribute determines when a label's text is wrapped onto multiple lines. The wrap length is always screen units. If you need to compute a wrapLength based on the font metrics, then you can use the font metrics command. If you use Tk 4.2 or earlier, then you have to measure text using a text widget with the same font. Chapter 36 describes the text widget operations that return size information for characters. You can force line breaks by including newlines (\n) in the label's text. This lets you create labels that have multiple lines of text. Label AttributesTable 32-2 lists the widget attributes for the label widget. The attributes are named according to their resource name, which includes a capital letter at internal word boundaries. When you specify an attribute as an option in a Tcl command when creating or reconfiguring a widget, however, you specify the attribute with a dash and all lowercase letters. Chapter 31 explains how to use resource specifications for attributes. Chapters 40, 41, and 42 discuss many of these attributes in more detail.
|
| [ Team LiB ] |
|