Resizing and -expand

The -expand true packing option lets a widget expand its packing space into unclaimed space in the packing cavity. Example 23-6 could use this on the small frame on top to get it to expand across the top of the display, even though it is packed to the right side. The more common case occurs when you have a resizable window. When the user makes the window larger, the widgets have to be told to take advantage of the extra space. Suppose you have a main widget like a text, listbox, or canvas that is in a frame with a scrollbar. That frame has to be told to expand into the extra space in its parent (e.g., the main window) and then the main widget (e.g., the canvas) has to be told to expand into its parent frame. Example 22-1 on page 316 does this.

In nearly all cases the -fill both option is used along with -expand true so that the widget actually uses its extra packing space for its own display. The converse is not true. There are many cases where a widget should fill extra space but not attempt to expand into the packing cavity. The examples below show the difference.

Now we can investigate what happens when the window is made larger. The next example starts like Example 23-7 on page 338, but the size of the main window is increased:

Example 23-11 Resizing without the expand option.

# Make the main window black
. config -bg black
# Create and pack two frames
frame .menubar -bg white
frame .body -width 150 -height 50 -bg grey50
# Create buttons at either end of the menubar
foreach b {alpha beta} {
   button .menubar.$b -text $b
}
pack .menubar.alpha -side left
pack .menubar.beta -side right
# Let the menu bar fill along the top
pack .menubar -side top -fill x
pack .body
# Resize the main window to be bigger
wm geometry . 200x100
# Allow interactive resizing
wm minsize . 100 50
					

The only widget that claims any of the new space is.menubar because of its -fill x packing option. The .body frame needs to be packed properly:

Example 23-12 Resizing with expand turned on.

# Use all of Example 23–11 then repack .body
pack .body -expand true -fill both
					

If more than one widget inside the same parent is allowed to expand, then the packer shares the extra space between them proportionally. This is probably not the effect you want in the examples we have built so far. The .menubar, for example, is not a good candidate for expansion.

Example 23-13 More than one expanding widget.

# Use all of Example 23–11 then repack .menubar and .body
pack .menubar -expand true -fill x
pack .body -expand true -fill both

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.249.90