See the section on grid in Tk Geometry management.
To be honest, although grid() seems easier and more intuitive to a new programmer, pack() is easier to use, especially when you want to easily handle window resizing by the user.
The primary issue is creating four equally sized frames within a main window
If that is the primary issue, look at how easy pack can do it. And remember, frames only get any size by the size of the widgets they contain. No widgets, no size for the frame. Notice in pack, the -expand and -fill options, which tell the pack() geometry manager how to handle resizes. Your can have just x or y filling with -fill=>'x" or -fill=> "y".
I hardly ever resort to using grid(), but I seem to recall it's resizing is a bit more complex to handle.
#!/usr/bin/perl
use strict;
use Tk;
use Tk::Pane;
my $mw = MainWindow->new;
$mw->geometry('400x250');
my $mwf = $mw->Scrolled('Pane',
-scrollbars=>'osoe',
-sticky=>'nwse',
)->pack(-expand=>1, -fill=>'both');
my %frame; # hash for holding frames
for (1..4){
$frame{$_} = $mwf->Frame()->pack(-expand=>1,-fill=>'both');
}
my %canv;
for (0..4){
$canv{$_}{'obj'} = $frame{1}->Scrolled('Canvas',
-height => 100,
-width => 100,
-bg =>'white',
-scrollbars => 'osoe',
-scrollregion => [ 0, 0, 500, 500 ],
)->pack(-side =>'left' ,-padx=>10,-pady=>10, -expand=>1, -fill=>'bo
+th');
$canv{$_}{'obj'}->createText(50,50,
-text => $_,
-anchor => 'center',
);
}
for (5..9){
$canv{$_}{'obj'} = $frame{2}->Scrolled('Canvas',
-height => 100,
-width => 100,
-bg =>'lightseagreen',
-scrollbars => 'osoe',
-scrollregion => [ 0, 0, 500, 500 ],
)->pack(-side =>'left', -padx=>10,-pady=>10 ,-expand=>1, -fill=>'bo
+th');
$canv{$_}{'obj'}->createText(50,50,
-text => $_,
-anchor => 'center',
);
}
for (6..10){
$canv{$_}{'obj'} = $frame{3}->Scrolled('Canvas',
-height => 100,
-width => 100,
-bg =>'yellow',
-scrollbars => 'osoe',
-scrollregion => [ 0, 0, 500, 500 ],
)->pack(-side =>'left', -padx=>10,-pady=>10 ,-expand=>1, -fill=>'bo
+th');
$canv{$_}{'obj'}->createText(50,50,
-text => $_,
-anchor => 'center',
);
}
for (11..15){
$canv{$_}{'obj'} = $frame{4}->Scrolled('Canvas',
-height => 100,
-width => 100,
-bg =>'hotpink',
-scrollbars => 'osoe',
-scrollregion => [ 0, 0, 500, 500 ],
)->pack(-side =>'left', -padx=>10,-pady=>10 ,-expand=>1, -fill=>'bo
+th');
$canv{$_}{'obj'}->createText(50,50,
-text => $_,
-anchor => 'center',
);
}
MainLoop();
|