#!/usr/bin/perl -w ############### ## Libraries ## ############### use strict; use warnings; use Tk; use Tk::Font; ################## ## Instructions ## ################## my $help = qq{ Instructions: (1) Click "Minimize" followed by "Maximize", and notice how the Canvas alternates between visible and hidden. (2) While the Canvas is maximized, resize the GUI with the mouse, by clicking on and dragging any corner of the GUI to a new place. (3) Finally, click "Minimize" again, and note that the GUI keeps the size of Maximized window, rather than shrinking as before. }; ################## ## Main Program ## ################## my $state = 'max'; # Create the Toplevel my $mw = new MainWindow(-title => "Pack/Repack Example"); # Assign colors, fonts, reliefs my $font1 = $mw->Font(-family => 'arial', -size => 24); my $font2 = $mw->Font(-family => 'arial', -size => 16); my @btn = (-font => $font1, -bg => '#ffef3f'); my @lbl = (-font => $font2, -bg => 'pink', -relief => 'groove'); # Assign widgets my $frm1 = $mw->Frame->pack(-fill => 'x'); my $frm2 = $mw->Frame->pack(-fill => 'x'); my $frm3 = $mw->Frame->pack(-expand => 1, -fill => 'both'); my $btn1 = $frm1->Button(@btn, -text => 'Minimize'); my $btn2 = $frm1->Button(@btn, -text => 'Maximize'); my $btn3 = $frm1->Button(@btn, -text => 'Exit (^X)'); my $can = $frm3->Canvas(-bg => 'white'); my $lbl = $frm2->Label(-text => $help, @lbl)->pack(-fill => 'x'); # Pack the buttons and canvas $btn1->pack($btn2, $btn3, -side => 'left'); $can->pack(-expand => 1, -fill => 'both'); # Draw some random circles in the Canvas for fun for (my $i = 0; $i < 512; $i++) { my $radius = 8 + int rand(64); my ($x0, $y0) = (int rand(800), int rand(800)); my ($x1, $y1) = ($x0 + $radius, $y0 + $radius); my $fill = sprintf "#%02x%02x%02x", rand 256, rand 256, rand 256; $can->createOval($x0, $y0, $x1, $y1, -fill => $fill); } # Attach callbacks and bindings $btn1->configure(-command => sub { minimize() }); $btn2->configure(-command => sub { maximize() }); $btn3->configure(-command => sub { exit }); $mw->bind("" => sub { $btn3->invoke }); MainLoop; ################# ## Subroutines ## ################# sub minimize { ($state eq 'min') and return; $state = 'min'; $frm3->packForget; } sub maximize { ($state eq 'max') and return; $state = 'max'; $frm3->packConfigure(-expand => 1, -fill => 'both'); }