O'Reilly logo

Mastering Perl/Tk by Nancy Walsh, Stephen Lidie

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Tk::Animation

Recall our circle bitmap from Figure 17-5 and how, with the addition of an inverted mask, we made the circle transparent. Using a timer event, we can make the bitmap cycle between solid and transparent so it flashes like a pulsar.

$mw->repeat(100 => sub {
    $b->configure(-maskfile => $count++ % 2 ? undef : 'circle.msk');
});

Some call this animation, crude as it may be.

Perl/Tk supplies an Animation widget in the standard distribution, designed to render a series of Photos, like frames in a movie film. It’s also smart enough to handle GIF89 images that contain multiple frames per image. Here’s a program that accepts a list of image filenames from the command line and animates them.

my $animate;
if (@ARGV) {
    $animate = $mw->Animation;
    foreach (@ARGV) {
        $animate->add_frame($mw->Photo(-file => $_));
    }
} else {
    my $gif89 = Tk->findINC('anim.gif');
    $animate = $mw->Animation(-format => 'gif', -file => $gif89);
}
$animate->set_image(0);

my $lab = $mw->Label(-image => $animate);

my $start = $mw->Button(
    -text => 'Start',
    -command => [$animate => 'start_animation', 500]);
my $stop  = $mw->Button(
    -text => 'Stop',
    -command => [$animate => 'stop_animation']);

If no images are specified, the program defaults to the multiframe “Powered by Perl” GIF89 image, as Figure 17-16 shows.

GIF89 images can be multiframe

Figure 17-16.  GIF89 images can be multiframe

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required