#!/usr/bin/perl -w
use Tk;
my $mw = MainWindow->new();
$mw->geometry ('320x240');
# Create a bottom-aligned frame for the button and a "main" frame for the
# rest of the window's contents.
my $bottom = $mw->Frame->pack (-side => 'bottom', -fill => 'x');
my $main = $mw->Frame->pack (-side => 'bottom', -fill => 'both', -expand => 1);
# For ease, the frames that'll appear and disappear will both be inside of a
# "wrapper" frame, which will contain only ONE of the two frames.
my $wrapper = $main->Frame->pack (-fill => 'both', -expand => 1);
# Create the two frames. Only pack one of them.
my $frame1 = $wrapper->Frame (-background => 'blue')->pack (-fill => 'both', -expand => 1);
my $frame2 = $wrapper->Frame (-background => 'red');
# Put something in them.
my $lab1 = $frame1->Label (
-text => 'Hello, world!',
-font => [
-family => 'Helvetica',
-size => 16,
-weight => 'bold',
],
-background => 'blue',
-foreground => 'white',
)->pack;
my $lab2 = $frame2->Label (
-text => 'Hello, mars!',
-font => [
-family => 'Times',
-size => 16,
-weight => 'bold',
],
-background => 'red',
-foreground => 'white',
)->pack;
# And a button to swap the frames.
my $switch = 0;
my $btn = $bottom->Button (
-text => 'Swap Frames',
-command => sub {
if ($switch == 0) {
$frame1->packForget();
$frame2->pack (-fill => 'both', -expand => 1);
$switch = 1;
}
else {
$frame2->packForget();
$frame1->pack (-fill => 'both', -expand => 1);
$switch = 0;
}
},
)->pack (-pady => 2);
MainLoop;