pack
Guide: pack, grid, and place
Puts widgets in a container in order, each one against a side of the space that is left.
pack WINDOW ?WINDOW ...? ?OPTION VALUE ...?
pack SUBCOMMAND ?ARG ...?
Description
The geometry manager pack keeps a list of widgets for each container. The layout goes through the list in order. Each widget gets a parcel at one side of the free space of the container, and the free space gets smaller. A widget at the top or the bottom gets the full width of the free space. A widget at the left or the right gets the full height.
A first word that starts with a dot is the same as pack configure. The container is the parent of the widget, or the value of -in. By default the container changes its requested size to the size that its list needs. pack propagate turns this off.
A toplevel cannot be packed. A widget has one geometry manager at a time. pack on a widget of grid or place makes pack its manager. A destroyed widget leaves the list.
Subcommands
| Subcommand | Description |
|---|---|
pack configure | Packs the widgets with the options, or changes the options of packed widgets. |
pack forget | Takes widgets out of the list of their container and off the screen. |
pack info | Gives the options of a packed widget. |
pack propagate | Gives or sets whether a container gets the size that its list needs. |
pack slaves | Gives the packed widgets of a container, in the order of its list. |
pack content | Gives the packed widgets of a container, in the order of its list. |
Options
| Option | Type | Default | Description |
|---|---|---|---|
-after | window name | empty | Puts the widgets after WINDOW in the list of its container. |
-anchor | anchor | center | The place of a widget in a parcel larger than the widget. |
-before | window name | empty | Puts the widgets before WINDOW in the list of its container. |
-expand | boolean | 0 | Gives the parcel of the widget a share of the extra space of the container. |
-fill | fill style | none | Stretches the widget to the width, the height, or both of its parcel. |
-in | window name | empty | The container of the widgets, in place of their parent. |
-ipadx | screen distance | 0 | Space added inside the widget at the left and the right. |
-ipady | screen distance | 0 | Space added inside the widget above and below. |
-padx | one or two screen distances | 0 | Free space outside the widget at the left and the right. |
-pady | one or two screen distances | 0 | Free space outside the widget above and below. |
-side | side | top | The side of the free space where the parcel of the widget goes. |
-after WINDOW
Puts the widgets after WINDOW in the list of its container.
WINDOW must be packed. The widgets go into the list of the container of WINDOW, after it, in the order of the call. The container of WINDOW is the container of the widgets, unless -in names another one.
% frame .af
> frame .af.a
> frame .af.b
> frame .af.c
> pack .af.a .af.b
> pack .af.c -after .af.a
> pack slaves .af
.af.a .af.c .af.b% frame .af2
> frame .af3
> pack .af2 -after .af3
window ".af3" isn't packed
-errorcode NONE-anchor ANCHOR
The place of a widget in a parcel larger than the widget.
When the parcel is larger than the widget and -fill does not stretch the widget, the anchor puts the widget at that side or corner of the parcel. center puts it in the middle.
Values: n, ne, e, se, s, sw, w, nw, center.
# The parcel of a widget at the top is as wide as the container, so `w` moves the widget to the left edge.
% frame .an -width 200 -height 100
> pack propagate .an 0
> pack .an
> frame .an.a -width 20 -height 20
> pack .an.a -anchor w
> update
> list [winfo x .an.a] [winfo y .an.a]
0 0% frame .an2
> pack .an2 -anchor middle
bad anchor "middle": must be n, ne, e, se, s, sw, w, nw, or center
-errorcode NONE-before WINDOW
Puts the widgets before WINDOW in the list of its container.
WINDOW must be packed. The widgets go into the list of the container of WINDOW, before it, in the order of the call.
% frame .bf
> frame .bf.a
> frame .bf.b
> pack .bf.a
> pack .bf.b -before .bf.a
> pack slaves .bf
.bf.b .bf.a% frame .bf2
> pack .bf2 -before .bf.none
bad window path name ".bf.none"
-errorcode NONE-expand BOOLEAN
Gives the parcel of the widget a share of the extra space of the container.
When the container is larger than its list needs, each widget with -expand 1 gets an equal share of the extra space in its parcel. The widget itself stays at its size, unless -fill stretches it.
# The parcel has all the space of the container, and the widget is at its center.
% frame .ex -width 200 -height 100
> pack propagate .ex 0
> pack .ex
> frame .ex.a -width 20 -height 20
> pack .ex.a -expand 1
> update
> list [winfo x .ex.a] [winfo y .ex.a]
90 40% frame .ex2
> pack .ex2 -expand maybe
expected boolean value but got "maybe"
-errorcode NONE-fill STYLE
Stretches the widget to the width, the height, or both of its parcel.
x makes the widget as wide as its parcel, y makes it as tall, and both does the two. none keeps the requested size of the widget. The internal padding -ipadx and -ipady is part of the requested size, and the external padding stays free.
Values: none, x, y, both.
% frame .fi -width 200 -height 100
> pack propagate .fi 0
> pack .fi
> frame .fi.a -width 20 -height 10
> pack .fi.a -fill x
> update
> winfo width .fi.a
200% frame .fi2
> pack .fi2 -fill all
bad fill style "all": must be none, x, y, or both
-errorcode NONE-in CONTAINER
The container of the widgets, in place of their parent.
CONTAINER must be the parent of the widget or a descendant of that parent. It cannot be the widget itself, or a widget inside the widget.
% frame .in1
> frame .in2
> pack .in2 -in .in1
> pack slaves .in1
.in2% frame .in3
> pack .in3 -in .in3
can't pack .in3 inside itself
-errorcode NONE
% frame .in4
> frame .in5
> frame .in5.x
> pack .in4.no -in .in5.x
bad window path name ".in4.no"
-errorcode NONE
% frame .in6
> frame .in7
> button .in7.b
> pack .in7.b -in .in6
can't pack .in7.b inside .in6
-errorcode NONE-ipadx DISTANCE
Space added inside the widget at the left and the right.
The widget gets a width of its requested width plus two times DISTANCE. The value must not be negative.
% frame .ip
> button .ip.b -text Hi
> pack .ip.b
> pack .ip
> update
> set w [winfo width .ip.b]
> pack .ip.b -ipadx 10
> update
> expr {[winfo width .ip.b] - $w}
20% frame .ip2
> pack .ip2 -ipadx -1
bad ipadx value "-1": must be positive screen distance
-errorcode NONE-ipady DISTANCE
Space added inside the widget above and below.
The widget gets a height of its requested height plus two times DISTANCE. The value must not be negative.
% frame .iy
> button .iy.b -text Hi
> pack .iy.b -ipady 4
> lindex [pack info .iy.b] 11
4% frame .iy2
> pack .iy2 -ipady x
bad ipady value "x": must be positive screen distance
-errorcode NONE-padx PAD
Free space outside the widget at the left and the right.
One distance puts the same space at the two sides. A list of two distances puts the first at the left and the second at the right. The space is part of the parcel and stays empty. The values must not be negative.
% frame .px
> frame .px.a -width 20 -height 20
> pack .px.a -padx 5
> pack .px
> update
> list [winfo reqwidth .px] [winfo x .px.a]
30 5
% frame .px2
> pack .px2 -padx {2 4}
> lindex [pack info .px2] 13
2 4% frame .px3
> pack .px3 -padx -1
bad pad value "-1": must be positive screen distance
-errorcode NONE
% frame .px4
> pack .px4 -padx {1 2 3}
wrong number of parts to pad specification
-errorcode NONE-pady PAD
Free space outside the widget above and below.
One distance puts the same space above and below. A list of two distances puts the first above and the second below. The values must not be negative.
% frame .py
> frame .py.a -width 20 -height 20
> pack .py.a -pady {1 3}
> pack .py
> update
> list [winfo reqheight .py] [winfo y .py.a]
24 1% frame .py2
> pack .py2 -pady x
bad pad value "x": must be positive screen distance
-errorcode NONE-side SIDE
The side of the free space where the parcel of the widget goes.
top and bottom give a parcel with the full width of the free space and the height of the widget. left and right give a parcel with the full height of the free space and the width of the widget.
Values: top, bottom, left, right.
% frame .sd
> frame .sd.a -width 30 -height 20
> frame .sd.b -width 30 -height 20
> pack .sd.a .sd.b -side left
> pack .sd
> update
> list [winfo x .sd.a] [winfo x .sd.b]
0 30% frame .sd2
> pack .sd2 -side middle
bad side "middle": must be top, bottom, left, or right
-errorcode NONEExamples
# The second frame is at the right of the first, and the container is as large as its list.
% frame .row
> frame .row.a -width 30 -height 20
> frame .row.b -width 30 -height 20
> pack .row.a .row.b -side left
> pack .row
> update
> list [winfo x .row.b] [winfo reqwidth .row] [winfo reqheight .row]
30 60 20
% frame .col
> button .col.ok -text OK
> pack .col.ok
> winfo manager .col.ok
pack
Errors
% pack
wrong # args: should be "pack option arg ?arg ...?"
-errorcode TCL WRONGARGS
% pack bogus .x
bad option "bogus": must be configure, content, forget, info, propagate, or slaves
-errorcode NONE
% pack .
can't pack ".": it's a top-level window
-errorcode NONE
% frame .p1
> frame .p2
> pack .p1 -in .p2.no
bad window path name ".p2.no"
-errorcode NONE
Differences from Tcl
- The errors of
packhave the-errorcodeNONE. Tk 8.6.16 gives a code such asTK PACK NOT_PACKEDorTK GEOMETRY TOPLEVEL. - A widget that
pack forgetremoved from its list keeps its old options in ztk. When it is packed again, each option that the call does not give has its old value. In Tk 8.6.16 the options go back to their defaults. pack forgetwith a name of a widget that does not exist gives an error in ztk. Tk 8.6.16 gives no error.- A distance such as
1ccan give a pixel count that differs by one from Tk, because the count of pixels for each centimeter differs.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1