in reply to Re: Tk::pack help
in thread Tk::pack help

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.