www.gibmonks.com




  Previous section   Next section

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

Table of Contents
Chapter 37.  Tk Widget Attributes


Size

Most widgets have a width and height attribute that specifies their desired size, although there are some special cases. In all cases, the geometry manager for a widget might modify the size to some degree. The winfo operations described on page 573 return the current size of a widget.

Most of the text-related widgets interpret their sizes in units of characters for width and lines for height. All other widgets, including the message widget, interpret their dimensions in screen units, which are pixels by default. The tk scale command, which is described on page 582, controls the scale between pixels and the other measures. You can suffix the dimension with a unit specifier to get a particular measurement unit:

c     centimeters
i     inch
m     millimeters
p     printer points (1/72 inches)

Scales and scrollbars can have two orientations as specified by the orient attribute, so width and height are somewhat ambiguous. These widgets do not support a height attribute, and they interpret their width attribute to mean the size of their narrow dimension. The scale has a length attribute that determines its long dimension. Scrollbars do not even have a length. Instead, a scrollbar is assumed to be packed next to the widget it controls, and the fill packing attribute is used to extend the scrollbar to match the length of its adjacent widget. Example 30-1 on page 430 shows how to arrange scrollbars with another widget.

The message widget displays a fixed string on multiple lines, and it uses one of two attributes to constrain its size: its aspect or its width. The aspect ratio is defined to be 100*width/height, and it formats its text to honor this constraint. However, if a width is specified, it just uses that and uses as many lines (i.e., as much height) as needed. Example 29-3 on page 423 shows how message widgets display text. Table 37-1 summarizes the attributes used to specify the size for widgets:

Table 37-1. Size attribute resource names.
aspectThe aspect ratio of a message widget, which is 100 times the ratio of width divided by height.
heightHeight, in text lines or screen units. Widgets: button, canvas, checkbutton, frame, label, listbox, menubutton, radiobutton, text, and toplevel.
lengthThe long dimension of a scale.
orientOrientation for long and narrow widgets: horizontal or vertical. Widgets: scale and scrollbar.
widthWidth, in characters or screen units. Widgets: button, canvas, checkbutton, entry, frame, label, listbox, menubutton, message, radiobutton, scale, scrollbar, text, and toplevel.

It is somewhat unfortunate that text-oriented widgets only take character- and line-oriented dimensions. These sizes change with the font used, and if you want a precise size you might be frustrated. Both pack and grid let the widgets decide how big to be. One trick is to put each widget, such as a label, in its own frame. Specify the size you want for the frame, and then pack the label and turn off size propagation. For example:

Example 37-1 Equal-sized labels.

graphics/37fig01.gif


proc EqualSizedLabels { parent width height strings args } {
   set l 0
   foreach s $strings {
      frame $parent.$l -width $width -height $height
      pack propagate $parent.$l false
      pack $parent.$l -side left
      eval {label $parent.$l.l -text $s} $args
      pack $parent.$l.l -fill both -expand true
      incr l
   }
}
frame .f ; pack .f
EqualSizedLabels .f 1i 1c {apple orange strawberry kiwi} \
   -relief raised

The frames $parent.$l are all created with the same size. The pack propagate command prevents these frames from changing size when the labels are packed into them later. The labels are packed with fill and expand turned on so that they fill up the fixed-sized frames around them.


      Previous section   Next section
    Top