Padding and Anchors

   

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

Table of Contents
Chapter 37.  Tk Widget Attributes


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.
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, 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.

graphics/37fig03.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 37-4 Anchoring text in a label or button.

graphics/37fig04.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 23 has several more examples of the packing parameters.

Putting It All Together

graphics/tip_icon.gif

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.

graphics/37fig05.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 

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.


       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net