Previous section   Next section

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

Table of Contents
Chapter 37.  Tk Widget Attributes

Padding and Anchors

Table 37-4 lists padding and anchor attributes that are similar in spirit to some packing attributes described in Chapter 23. However, they are distinct from the packing attributes, and this section explains how they work together with the packer

Table 37-4. Layout attribute resource names.

The anchor position of the widget.

Values: n, ne, e, se, s, sw, w, nw, or center.

Widgets: button, checkbutton, label, menubutton, message, or radiobutton.

padX, padY

Padding space in the X or Y direction, in screen units.

Widgets: button, checkbutton, label, menubutton, message, radiobutton, or text.

The padding attributes for a widget define space that is never occupied by the display of the widget's contents. For example, if you create a label with the following attributes and pack it into a frame by itself, you will see the text is still centered, despite the anchor attribute.

Example 37-3 Padding provided by labels and buttons.


label .foo -text Foo -padx 20 -anchor e
pack .foo

The anchor attribute only affects the display if there is extra room for another reason. One way to get extra room is to specify a width attribute that is longer than the text. The following label has right-justified text. You can see the default padX value for labels, which is one pixel:

Example 37-4 Anchoring text in a label or button.


label .foo -text Foo -width 10 -anchor e
pack .foo

Another way to get extra display space is with the -ipadx and -ipady packing parameters. The example in the next section illustrates this effect. Chapter 23 has several more examples of the packing parameters.

Putting It All Together


The number of different attributes that contribute to the size and appearance can be confusing. The example in this section uses a label to demonstrate the difference among size, borders, padding, and the highlight. Padding can come from the geometry manager, and it can come from widget attributes:

Example 37-5 Borders and padding.


frame .f -bg white
label .f.one -text One -relief raised -bd 2 -padx 3m -pady 2m
pack .f.one -side top
label .f.two -text Two \
   -highlightthickness 4 -highlightcolor red \
   -borderwidth 5 -relief raised \
   -padx 0 -pady 0 \
   -width 10 -anchor nw
pack .f.two -side top -pady 10 -ipady 10 -fill both
focus .f.two
pack .f

The first label in the example uses a raised relief, so you can see the two-pixel border. (The default border size changed in Tk 8.0 to one pixel to match the CDE look and feel.) There is no highlight on a label by default. There is internal padding so that the text is spaced away from the edge of the label. The second label adds a highlight rectangle by specifying a nonzero thickness. Widgets like buttons, entries, listboxes, and text have a highlight rectangle by default. The second label's padding attributes are reduced to zero. The anchor positions the text right next to the border in the upper-left (nw) corner. However, note the effect of the padding provided by the packer. There is both external and internal padding in the Y direction. The external padding (from pack -pady) results in unfilled space. The internal packing (pack -ipady) is used by the label for its display. This is different from the label's own -pady attribute, which keeps the text away from the top edge of the widget.

      Previous section   Next section