www.gibmonks.com




  Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 29.  Simple Tk Widgets


The Message Widget

The message widget displays a long text string by formatting it onto several lines. It is designed for use in dialog boxes. It can format the text into a box of a given width, in screen units, or a given aspect ratio. The aspect ratio is defined to be the ratio of the width to the height, times 100. The default is 150, which means that the text will be one and a half times as wide as it is high.

Example 29-3 creates a message widget with one long line of text. Backslashes are used to continue the text string without embedding any newlines. (You can also just type a long line into your script.) Note that backslash-newline collapses white space after the newline into a single space.

Example 29-3 The message widget formats long lines of text.

graphics/29fig01.gif


message .msg -justify center -text "This is a very long text\
   line that will be broken into many lines by the\
   message widget"
pack .msg

graphics/tip_icon.gif

A newline in the string forces a line break in the message display. You can retain exact control over the formatting by putting newlines into your string and specifying a very large aspect ratio. In Example 29-4, grouping with double quotes is used to continue the string over more than one line. The newline character between the quotes is included in the string, and it causes a line break:

Example 29-4 Controlling the text layout in a message widget.

graphics/29fig02.gif


message .msg -aspect 1000 -justify left -text \
"This is the first long line of text,
and this is the second line."
pack .msg

One disadvantage of a message widget is that, by default, you cannot select the text it displays. Chapter 35 describes how to define custom selection handlers, so you could define one that returned the message string. The message widget predates the text widget, which has many more features and can emulate the message widget. If selections, multiple fonts, and other formatting are important, use a text widget instead of a message widget. Text widgets are described in Chapter 33.

Message Attributes

Table 29-3 lists the attributes for the message widget. The table lists the resource name, which has capitals at internal word boundaries. In Tcl commands these options are specified with a dash and all lowercase:

Table 29-3. Message Attributes.
anchorRelative position of the text within its packing space.
aspect100 * width / height. Default 150.
backgroundBackground color (also bg).
borderWidthExtra space around the edge of the text.
cursorCursor to display when mouse is over the widget.
fontFont for the message's text.
foregroundForeground color (also fg).
highlightBackgroundFocus highlight color when widget does not have focus.
highlightColorFocus highlight color when widget has focus.
highlightThicknessThickness of focus highlight rectangle.
justifyJustification: left, center, or right.
padXExtra space to the left and right of the text.
padYExtra space above and below the text.
reliefflat, sunken, raised, groove, solid or ridge.
takeFocusControls focus changes from keyboard traversal.
textText to display.
textVariableName of Tcl variable. Its value is displayed.
widthWidth, in screen units.

Arranging Labels and Messages

Both the label and message widgets have attributes that control the position of their text in much the same way that the packer controls the position of widgets within a frame. These attributes are padX, padY, anchor, and borderWidth. The anchor takes effect when the size of the widget is larger than the space needed to display its text. This happens when you specify the -width attribute or if you pack the widget with fill enabled and there is extra room. See Chapter 37 and the section on Padding and Anchors for more details.


      Previous section   Next section
    Top