Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tk window resizing

Status
Not open for further replies.

AMiSM

Technical User
Jan 26, 2006
128
US
Greetings!
Is there any way to cause widgets to fill a window, maintaining proprtions to each other, when the window is resized (e.g. - maximised)?
Can references be used when sizing things, and the variables update themselves?
Is there any way to retrieve the current proportions of the window?
 
Organizing Tk widgets is a pretty tricky thing. When using pack, you can use -fill and -expand. Here's a kind of quick example to have a few widgets that maintain their sizes and have one that fills the remaining space (this isn't tested though since I'm not where I can use Perl right now).

Code:
my $mw = MainWindow->new;
$mw->geometry ('640x480');

# The overall layout of the window will look like:
# [=============]
# [        ][xxx]
# [        ][xxx]
# [        ][xxx]
# where =,  , and x are three different frames,
# and = and x won't resize themselves when the
# window resizes, but the big widget will.

# The top frame ([===]) goes at the top of the window
# and fills across horizontally.
my $topFrame = $mw->Frame->pack (-side => 'top', -fill => 'x');

# This wrapper frame will contain the large frame
# and the [xxx] frame.
my $wrapperFrame = $mw->Frame->pack (-side => 'top', -fill => 'both', -expand => 1);

# The right frame goes within the wrapper frame.
my $rightFrame = $wrapperFrame->Frame->pack (-side => 'right', -fill => 'y');

# The main frame also goes in the wrapper frame.
my $mainFrame = $wrapperFrame->Frame->pack (-side => 'right', -fill => 'both', -expand => 1);

# Now create a few widgets in each frame.
$topFrame->Label (
   -text => 'Just a simple label',
)->pack (-side => 'left');
$topFrame->Button (
   -text => 'And a button',
)->pack (-side => 'left');
$rightFrame->Label (
   -text => "The width of this label\n"
      . "will ultimately\n"
      . "control the width\n"
      . "of the right frame.",
)->pack (-side => 'top');
$mainFrame->Scrolled ('Text',
   -scrollbars => 'osoe',
)->pack (-fill => 'both', -expand => 1);

That should create a window with a label and button in the top left, a label placed toward the right side of the window but below the bottom of that top frame, and the remaining space should be filled with a large text field that will resize when the window resizes.

Resizing the window would keep the two frames where they're at, and the frame containing the text box should resize to fill all the space that it can.

For the most part, don't use -expand too often, because -expand makes the widget try to take up as much space as it possibly can. For example, if you have one label that -fills "x" and -expands, and then have one below that which -fills "both" and -expands, your window will be divided in half, with each widget being in one half of it. The bottom widget would fill its entire half of the window, the top widget would only cover horizontally but leave a bunch of wasted space above and below it.

So it takes a little bit of tweaking until you get it to work, but it's possible.
 
Is there anything like that with "grid"?
 
There might be, but I haven't used grid at all until just in my last Tk app, so I'm not very familiar with it... it seems though that gridded items try to center themselves as much as possible, so that:

Code:
->grid (-column => 0, -row => 0);
->grid (-column => 1, -row => 0);
->grid (-column => 0, -row => 1);
->grid (-column => 1, -row => 1);

would center the widgets in the window so that if you made the window really wide:
Code:
|        widget1 widget2       |
|        widget3 widget4       |

Anyway, the trick is to just play around with it.

One idea is that you can pack() the frames and then grid() the widgets within each frame. I think that would be okay.

-------------
Kirsle.net | Kirsle's Programs and Projects
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top