Reputation: 457
I am relatively new to Tcl/Tk but have been working with it fairly successfully, non-stop for the last several weeks. For the most part it seems fairly straightforward. However, I've hit a snag recently which is probably simple but I can't seem to figure it out.
I have reduced my fairly complicated code to a few lines which demonstrate the problem.
My simple example produces two labelframes with a simple label widget in each. When I resize (i.e., expand) the main window, I want the “left” frame to resize in the y-direction only and I want the “middle” frame to resize in x and y. Those parts work fine.
However, I also want the frames to stay “stuck” to each other when resized. That is, I want the “middle” frame to stay stuck up against the “left” frame when they both resize. Instead, I see a gap expanding between the two.
Again, this is a simplified bit of code from a more complicated program. I am trying to avoid mixing grid and pack commands in the same program and, in the original more complicated program, using grid became very unwieldy. So a solution using “pack” would be preferred. But proper explanation would be a good start.
My small example:
#!/usr/bin/wish
## Set up two main frames
set frameLeft [labelframe .frameLeft]
set frameMiddle [labelframe .frameMiddle]
pack $frameLeft -fill y -expand 1
pack $frameMiddle -fill both -expand 1
pack $frameLeft $frameMiddle -side left -anchor nw
## Instead of the previous 3 lines, I also tried:
#pack $frameLeft -fill y -expand 1 -side left -anchor nw
#pack $frameMiddle -fill both -expand 1 -side left -anchor nw
## Set up component in LEFT frame
set frameOne [labelframe $frameLeft.frameOne]
set labelOne [label $frameOne.labelOne -text "Label One:"]
pack $labelOne -anchor nw
pack $frameOne -anchor nw
## Set up component in MIDDLE frame
set frameTwo [labelframe $frameMiddle.frameTwo]
set labelTwo [label $frameTwo.labelTwo -text "Label Two:"]
pack $labelTwo -side left
pack $frameTwo -anchor nw
Upvotes: 1
Views: 1869
Reputation: 137587
First off, it usually helps when debugging a complex layout to put garish colors on the frames. Sure, you won't leave them in production, but it does make understanding the behavior of the geometry manager you're using much simpler.
When I try your code, I see that the two widgets are both being allocated the same amount of space in the horizontal direction; the .frameLeft
widget is then sticking to the left side of that space whereas the .frameMiddle
widget is filling out the space it has been allotted. So the problem isn't the behavior of the widgets within their “space packet”, but rather the allocation of space.
Now, with pack
, the allocation of the extra space is controlled by the -expand
option; the extra room is shared between the packets that have expansion enabled for them. This means that to get the layout you want, you should simply not set -expand 1
for .frameLeft
.
Upvotes: 3
Reputation: 1551
I understand that you prefer a "pack" answer, but grid is usually much easier to deal with since the "sticky" option handles the filling and expansion in an easier to handle semantic. I'm a little surprised that you had trouble with it. Unfortunately, I'm locked into "grid"-think. So does the following "grid" solution do what you are looking for?
#!/usr/bin/env wish
## Set up two main frames
set frameLeft [labelframe .frameLeft]
set frameMiddle [labelframe .frameMiddle]
grid $frameLeft -row 0 -column 0 -sticky nse
grid $frameMiddle -row 0 -column 1 -sticky nsew
grid columnconfigure . {0 1} -weight 1
grid rowconfigure . 0 -weight 1
## Set up component in LEFT frame
set frameOne [labelframe $frameLeft.frameOne]
set labelOne [label $frameOne.labelOne -text "Label One:"]
grid $frameOne -sticky nw
grid $labelOne -sticky nw
## Set up component in MIDDLE frame
set frameTwo [labelframe $frameMiddle.frameTwo]
set labelTwo [label $frameTwo.labelTwo -text "Label Two:"]
grid $frameTwo -sticky nw
grid $labelTwo -sticky nw
Upvotes: 0