Tk Source Code

View Ticket
Login
Ticket UUID: 222018
Title: grid does not always honor -sticky
Type: Bug Version:
Submitter: nobody Created on: 2000-11-09 02:45:57
Subsystem: 49. [grid] Assigned To: nobody
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2001-03-28 00:00:04
Resolution: Invalid Closed By: dgp
    Closed on: 2001-03-27 17:00:04
Description:
When building two frames that are packed to expand to
fit their container, and filling them with different 
numbers of widgets, grid .frame$x.widget$y -sticky nw,
the widgets in the less filled frame cling to the bottom
instead of the top.

This code (perhaps too complex) demonstrates the behavior.
The pack commands do what I expected the grid commands
to do - stack the widgets from top to bottom in both 
windows.  The grid command fills the left frame, and the
widgets cluster to the bottom in the right hand frame.

########## Demonstrate unexpected grid behavior
########## pack and grid code should generate equivalent
########## displays, but do not.

catch {destroy .c} 

canvas  .c -height 200 -width 100 -background white
pack .c
frame .c.f
.c create window 1 1 -anchor nw -window .c.f

frame .c.f.f1 -background pink
frame .c.f.f2 -background yellow

grid .c.f.f1 -row 0 -column 0 -sticky news
grid .c.f.f2 -row 0 -column 1 -sticky news

for {set i 0} {$i < 10} {incr i} {
  checkbutton .c.f.f1.b$i -text $i -textvar foo1
  grid .c.f.f1.b$i -row $i -column 1 -sticky nw 
#   pack .c.f.f1.b$i -side top -anchor w
}
 
for {set i 0} {$i < 8} {incr i} {
  checkbutton .c.f.f2.b$i -text $i -textvar foo2
  grid .c.f.f2.b$i -row $i -column 1 -sticky nw 
#   pack .c.f.f2.b$i -side top -anchor w
}
User Comments: ericm added on 2000-11-10 05:41:03:
This is not a bug, rather, this is a misunderstanding of how [grid] is supposed to work.

One of the big differences between [pack] and [grid] is how widgets are positioned when the available space is greater than is required by the widgets; [pack] anchors the layout to one side, but [grid] centers it.  This is exactly what the user is seeing in this case.

Briefly:  when [grid]ding a widget, that widget is put into a "container".  The sticky values affect where within the container the widget is positioned.  Normally, the container is "shrink-wrapped" around the widget.  If you specify that the container should grow, you will be able to see the effect of the sticky values; otherwise, you will not, and since the widget layout is centered, it looks like the sticky values are not working.

The user should add:

  grid rowconfigure .c.f.f2 7 -weight 1

after the second loop.  This will force the extra space to be allocated to the last row of widgets, effectively the same as what [pack] does, and will produce the result that the user is expecting.

Brent Welch's "Practical Programming in Tcl and Tk, 3rd Edition" has a good explanation of [pack] and [grid].  That text can probably explain better what is happening in this instance.  Also, read over the [pack] and [grid] manual entries for more information.

Eric Melski
The Other Tcl Guy
ericm at interwoven.com