in reply to Tk::pack help

Heya,

i played around a little bit with pack, but it seems to be impossible with a single pack. I could imagine to ways of solving it:

1. As some monks already mentioned, grid would be the geometry manager, which is most suitable for this task.

2. Untested: If it is possible to insert a frame into a frame, you could try the following code:
my $topframe = $mainWindow->Frame; $topframe->pack(-expand => 'x', -fill => 1); my $leftframe = $topframe->Frame; $leftframe->pack(-side => 'left', -expand => 'x', -fill =>1); my $rightframe = $topframe->Frame; $rightframe->pack(-side => 'left', -expand => 'x', -fill =>1);
... but using grid is much easier to and to understand.

Elderian

Replies are listed 'Best First'.
Re^2: Tk::pack help
by eserte (Deacon) on Aug 06, 2004 at 19:28 UTC
    With this approach vertical alignment of the widgets in the frames could be difficult. Here's a hack to use pack() with helper frames to form a gridded structure.
    #!/usr/bin/perl use strict; use List::Util qw(max); use POSIX qw(ceil); use Tk; my $mw = new MainWindow(); my @fields = ("Name", "Street", "Postal code", "City"); my @l; for (@fields) { my $f = $mw->Frame->pack(-fill => "x"); my $l = $f->Label(-text => $_, -anchor => "w")->pack(-side => "lef +t"); push @l, $l; my $e = $f->Entry->pack; } my $maxwidth = max map { $_->reqwidth } @l; my $fontwidth0 = $mw->fontMeasure($l[0]->cget("-font"), "0"); my $maxwidth_chars = ceil($maxwidth / $fontwidth0); for (@l) { $_->configure(-width => $maxwidth_chars); } MainLoop;
    The hack is to set the -width option of the labels to get a uniform width for the left column. In the sample script above this is done dynamically by extracting the width of the largest label. Unfortunately specification of widths is done in characters, but the reqwidth() method returns pixels, so an additional conversion is necessary.