jack123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I want to write a code to hide and show gif image on a click of button. Regards, Jack
#!/usr/bin/perl use strict; use warnings; use Tk::Animation; my $gif = 'C:/wamp/bin/Perl/bin/ajax_loader.gif'; my $mw = MainWindow->new; my $img = $mw->Animation( '-format' => 'gif', '-file' => $gif, ); $mw->Label('-image' => $img)->pack; my $lab = $mw->Label('-text' => "Please wait a moment while we fetch y +our setting information.......")->pack; $img->start_animation(100); my $start = $mw->Button(-text => 'Restart', -command => ['start_animat +ion' => $img, 50])->pack; my $stop = $mw->Button(-text => 'Stop', -command => ['stop_animation' + => $img, $lab='', $img->set_disposal_method(0)])->pack; my $pause = $mw->Button(-text => 'Pause', -command => ['pause_animatio +n' => $img])->pack; my $resume = $mw->Button(-text => 'Resume', -command => ['resume_anima +tion' => $img, 50])->pack; my $ff = $mw->Button(-text => '>>', -command => ['fast_forward' => $im +g,2])->pack; my $fr = $mw->Button(-text => '<<', -command => ['fast_reverse' => $im +g,-2])->pack; my $quit = $mw->Button(-text => 'Quit', -command => [destroy => $mw]) +->pack; $img->set_disposal_method(1); $mw->MainLoop;

Replies are listed 'Best First'.
Re: How to hide/show gif on click
by kcott (Archbishop) on May 11, 2012 at 08:59 UTC

    Here's a piece of working Tk code that shows how to hide and show a widget (on the click of a button) using pack() and packForget().

    #!/usr/bin/env perl use strict; use warnings; use Tk; my $mw = MainWindow->new; my $f1 = $mw->Frame()->pack; my $f2 = $mw->Frame()->pack; my $l1 = $f1->Label(-text => 'Image here')->pack; $f2->Button(-text => 'Show', -command => sub { $f1->pack(-before => $f +2); })->pack; $f2->Button(-text => 'Hide', -command => sub { $f1->packForget; })->pa +ck; $f2->Button(-text => 'Quit', -command => sub { exit; })->pack; MainLoop;

    -- Ken

Re: How to hide/show gif on click
by zentara (Cardinal) on May 11, 2012 at 09:47 UTC
    A canvas widget is also a way. You can also use the canvas tag method lower and raise to hide or show any canvas item, but this example just does it straightforward.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Animation; my $scr = new MainWindow; $scr->configure(-background=>"black"); $scr->geometry("200x150"); my $canvas = $scr->Canvas(-width,200,-height,100, -background=> "black")->pack(-expand, 1, -fill, 'both'); my $image = $scr->Animation('-format' => 'gif', -data => get_gif() + ); $canvas->createImage( 50,50, -image=> $image); $image->blank; my $buttonframe = $scr->Frame()->pack(-expand=>1, -fill=>'both'); my $start = $buttonframe->Button(-text => 'Start', -command => sub{ $image->start_animation(40); } )->pack(-side=>'left'); my $stop = $buttonframe->Button(-text => 'Stop', -command => sub{ $image ->stop_animation; $image ->blank } )->pack(-side=>'right'); MainLoop; sub get_gif{ #base64encoded gif89a ... Tk accepts images in base64encoded format as + -data my $gif = 'R0lGODlhIAAgAPMAAP////zzBf9kAt0IBvIIhEYApQAA1AKr6h+3FABkEVYsBZBxOsDAw +ICAgEBA QAAAACH/C05FVFNDQVBFMi4wAwEAAAAh/h1HaWZCdWlsZGVyIDAuMSBieSBZdmVzIFBpZ3 +VldAAh +QQECgD/ACwAAAAAIAAgAEMEgxC82SS4OOvNb6sfVT1dyX1AQ6YhuZpwh7agyL5xjoFf5d +U606x3 CwZrvh1vgjNqesOm8xSdSXXWTXbKSj2iV67y9g2JlcPuuat6tizrHzn+i9Iz4F74uPx6z1 +BEW05I bn+EQBhMI1xZfXsxVnZrgZN3bCplcGKCKjOALChynImXbDgRACH5BAQKAP8ALAAAAAAgAC +AAQwSG sLUHqr04a9uAlFw3baQGfuJYrpv4dNdJndPbUewqV99D4bkgL+VzCUk74sujOupoNg/Quc +NUnUwp dIo1KXvd2Cd7pRotvmV4qE0xuWu2pDiPk2vnLmpcFrpgIVJYZWk3ekZKcH5/RHZ7UHZWX4 +ZrfBN9 fkwgcopPgpIeh589nUEykHGnIhEAIfkEBAoA/wAsAAAAACAAIABDBIywtfckeCDrzbu2Uy +ZNGOad aAeSDRCmsNiGM2m64zffMbquvF5v19qAgkLOaInD1Cq75OlljEp1LmgTeVU9odSubIXLis +thSuVy Rmsl3LYMzTLLlzW7eFLEhpNWGWotcTBHLluFMUdAcjh4TY5KXxaKi0CHe499mVKdfpYemS +MUbHtE X3c1jZJ+f44gEQAh+QQECgD/ACwAAAAAIAAgAEMEhhDI1kB7MuvNNbVURWFdaW7iU3np6W +ZhDJIZ GT4Y/e6weKkWHY/zuVhmuKEyNFv1gsrOpfiLvqi4mxXFpEK3RR0OKFymsFsXVuVMH2Xcr1 +szrpSX GN94ftbPQTF+fyxIH3dDXVMtYIBOYVaPcUZRkTc5aWFrfD5og08jbG2YekB/Mh+DZxkRAD +s='; return $gif; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh