Node:fill-ing, Next:expand-ing, Previous:pad-ding, Up:Geometry managers
fill
-ingRemember that space is allocated to a widget from the currently available space left in the master widget by cutting off a complete slice from that space. It is often the case that the slice is bigger that the widget to be displayed in it.
There are further options for allowing a widget to fill the whole slice
allocated to it. This is done through the -fill
option, which
can have one of four values: none
for no filling (default),
x
to fill horizontally only, y
to fill vertically only,
and both
to fill both horizontally and vertically at the same
time.
Filling is useful, for example, for creating buttons that are
the same size even though they display texts of differing lengths.
To take our button example again, the following code produces
three buttons, one on top of each other, but of the same size:
button .b1 -text b1 button .b2 -text "hello world" button .b3 -text b3 pack .b1 .b2 .b3 -fill x
fill
For Evenly Sized Widgets.b2
because it has the widest text. Because the three
buttons are packed from top to bottom, then the slices of space
allocated to them are cut progressively straight along the top of the
remaining space. i.e. each widget gets a horizontal slice of space the
same width cut from the top-level widget. Only the wide button
.b2
would normally fit the whole width of its slice. But by
allowing the other two widgets to fill horizontally, then they will also
take up the whole width of their slices. The result: 3 buttons stacked
on top of each other, each with the same width, although the texts they
display are not the same length.
A further common example is adding a scrollbar to a listbox.
The trick is to get the scrollbar to size itself to the listbox;
see also library('tcltk/examples/ex9a.tcl')
:
listbox .l scrollbar .s pack .l .s -side left
pack .s -fill y
Now the display looks like a normal listbox with a scrollbar.
pack .s -fill y
command) allows it to fill its space, and so
it adjusts to the height of the listbox.