[ Team LiB ] Previous Section Next Section

Padding and Anchors

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

Table 40-4. Layout attribute resource names

anchor

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, frame, label, labelframe, menubutton, message, radiobutton, text, or toplevel.

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 40-3 Padding provided by labels and buttons

graphics/40inf03.gif

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 40-4 Anchoring text in a label or button

graphics/40inf04.gif

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 25 has several more examples of the packing parameters.

Putting It All Together

Example 40-5 Borders and padding

graphics/40inf05.gif

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

graphics/common_icon.gif

The number of different attributes that contribute to the size and appearance can be confusing. Example 40-5 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.

The first label uses a raised relief, so you can see the two-pixel border. 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. 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.

    [ Team LiB ] Previous Section Next Section