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();
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.