Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

TK Geometry Management

by shortyfw06 (Beadle)
on Mar 16, 2012 at 15:36 UTC ( #960001=perlquestion: print w/replies, xml ) Need Help??
shortyfw06 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to understand how the ->grid options work when organizing frames in the main window. Can you please suggest a good reference on this topic? The primary issue is creating four equally sized frames within a main window. Thank you.

Replies are listed 'Best First'.
Re: TK Geometry Management
by zentara (Archbishop) on Mar 16, 2012 at 16:28 UTC
    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();

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: TK Geometry Management
by kcott (Chancellor) on Mar 16, 2012 at 16:47 UTC

    In my opinion, the best reference work is still the book: Mastering Perl/Tk. Its predecessor, Learning Perl/Tk, coming a close second. Both of these books devote an entire chapter to geometry management.

    In my experience, it's best to use the same geometry manager within a Frame; use Frames to embed a different geometry manager. For instance, several Frames may be pack()ed; any of those Frame widgets may contain a set of widgets using a different geometry manager - mixing geometry managers within Frames typically results in tears.

    To illustrate further, code like this is generally fine:

    my $f1 = $mw->Frame(...)->pack; my $f2 = $mw->Frame(...)->pack; my $f3 = $mw->Frame(...)->pack; ,,, my $b1 = $f2->Button(...)->grid; my $b2 = $f2->Button(...)->grid;


    my $b1 = $f2->Button(...)->pack; my $b2 = $f2->Button(...)->grid;

    is typically problematical.

    The documentation for individual geometry managers is linked from: CPAN - Tk.

    Update: I had 7 instances of mismatched brackets: s/{...)/(...)/ - thanks moritz; s/.-/->/; s/=/->/

    -- Ken

Re: TK Geometry Management
by thundergnat (Deacon) on Mar 16, 2012 at 16:59 UTC

    Here's another example for comparison. (with some added silliness.) The frames grow/shrink when you resize the main window.

    use warnings; use strict; use Tk; my %w; $w{mw} = MainWindow->new; my ($initial_height, $initial_width) = (60, 100); my @quadrants = qw/tl tr bl br/; my @color = qw/red yellow green orange blue black white/; { my ($row, $col) = (0,0); for my $frame (@quadrants){ $w{frame}{$frame} = $w{mw}->Frame( -borderwidth => 3, -relief => 'groove', -height => $initial_height, -width => $initial_width, )->grid( -row => $row, -column => $col, -sticky => 'nsew' ); $w{frame}{$frame}->bind('<Configure>', \&update_frames); $col++; $row++ unless $col %= 2; } } $w{mw}->repeat(150, sub { $w{frame}{$quadrants[rand @quadrants]}-> configure(-background => $color[rand @color]) } ); MainLoop; sub update_frames{ return if $w{updating}; $w{updating} = 1; my ($w,$h) = split /[x+]/, $w{mw}->geometry; for my $frame (qw/tl tr bl br/){ $w{frame}{$frame}->configure( -width => $w / 2 - 2, -height => $h / 2 - 2 ) } $w{mw}->update; $w{updating} = 0; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://960001]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2017-06-22 23:24 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (531 votes). Check out past polls.