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

Dear monks, again I come to you for help and guidance. (This site is great! You're always so helpful.) Here's what I'm trying to do: have a Tk::Animation on a canvas. It looked so easy in the pod and on CPAN, but no. All I get is a black canvas. No errors or warnings.

I checked i) if "tree.gif" is there; ii) if it's GIF89; iii) that I put it on a canvas (I tried a label as well). I'm on MSWin32, perl 5.8.4 (ActiveState).
my $scr = new MainWindow; $scr->configure(-background=>"black"); $scr->geometry("500x500"); my $canvas = $scr->Canvas( -background=>"black", -width=>$scr->width, -height=>$scr->height, )->pack( -side=>"left" ); $canvas->Animation('-format' => 'GIF', -file => "tree.gif")->start +_animation( 20 );
Now what?

Replies are listed 'Best First'.
Re: Tk::Animation won't work :(
by JamesNC (Chaplain) on May 13, 2005 at 17:23 UTC
    Here you go:
    use Tk; use Tk::Animation; use strict; my $scr = new MainWindow; $scr->configure(-background=>"black"); $scr->geometry("500x500"); my $canvas = $scr->Canvas(-width,500,-height,500, -background=> "black")->pack(-expand, 1, -fill, 'both'); my $image = $scr->Animation('-format' => 'gif', -file => "tree.gif"); $canvas->createImage( 250,250, -image=> $image); $image->blank($image,1); $image->start_animation(40); MainLoop;


    JamesNC
      Yay! It works! Thanks.
        You are most welcome :^)
Re: Tk::Animation won't work :(
by zentara (Cardinal) on May 13, 2005 at 18:21 UTC
    Here is another example, in case you don't want to use a Canvas:
    #!/usr/bin/perl -w use Tk; use Tk::widgets qw/Photo Animation/; use strict; my $mw = MainWindow->new; 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']); my $quit = $mw->Button( -text => 'Quit', -command => \&exit); $lab->grid(-column => 1, -sticky => 'nsew'); $start->grid($stop, $quit, -sticky => 'ew'); $lab->gridRowconfigure(0,-weight => 1); MainLoop;

    I'm not really a human, but I play one on earth. flash japh
Re: Tk::Animation won't work :(
by zentara (Cardinal) on May 13, 2005 at 19:28 UTC
    Being a Friday afternoon, I just had to see if an animated gif could be read from -data. :-) It can, and makes a nice indicator.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Animation; my $scr = new MainWindow; $scr->configure(-background=>"black"); $scr->geometry("200x100"); 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($image,1); $image->start_animation(40); MainLoop; sub get_gif{ #base64encoded gif89a my $gif = 'R0lGODlhEAAQAPEEAAAAAP8AAP//AP///yH/C05FVFNDQVBFMi4wAwEAAAAh+QQFCgAEA +CwAAAAA EAAQAAADPki63B4wOhWrZFYEfWm2SwCMZDkGiglsajqU2viOablBJkVCnHhSGoFgYBGhgM +Me7ugR KlfM0DPaqKwmWEcCACH5BAUKAAQALAEAAAAPAA8AAAM8SKrR+ysA0CokM1cXwcjUxoCZYF +oNOZ1O BQqTGAiVScebMOwnWdsuj6ZB26gYmxQJmZRkIE5j4EKQJB8JACH5BAUKAAQALAEAAQAOAA +4AAAM3 SBoMzioy4cYLMojgOsOTQHXAFw4baZ7NtYap9prU1ryezZnqR+wcgKXU+O1IRMwi2ItkPE +pCAgAh +QQFCgAEACwBAAEADwAPAAADO0ga3KyQNEEZCHGKYYFfzhZ4wHBJFyOSJOGFAvs6aszSMI +nfnrDL gMpjRDJdhBjUjRaRMSOuWQOaeVATACH5BAUKAAQALAEAAQAOAA4AAAM2SBoB/Coy9wST7Q +XB79Tb 0H2gaFkNQG2TmqqUBc/A4AqzTQMy/e4wEAMFImhOxYUQEiGsNJEEACH5BAUKAAQALAAAAQ +APAA8A AAM8SErRDW2tAB2o8l7Hg9Ja5xDgxgnWNZiB4KIP2ApDDafzjTKpIEcOV8nEyw0hig5o5Z +lwSpLk Exl1RiQJACH5BAUKAAQALAEAAQAOAA4AAAM4SBoBzkFJ5ipgk9qGrx4PB2khBQlNCXmBAK +BjjF5C HY8VM9jxJuywlaUGIwhzxUUK9MJIjCmWJAEAIfkEBQoABAAsAAAAAA8ADwAAAz1IutGxUL +kGaiQz 1A2z3sCDNYLwDeDjlODmCdUXZ1vpOa3ttYBOAQReYGCiqACoGDGjSA19nVCgVBR1bosEAD +s='; return $gif; }

    I'm not really a human, but I play one on earth. flash japh
      Neat!
Re: Tk::Animation won't work :(
by Popcorn Dave (Abbot) on May 13, 2005 at 16:35 UTC
    A couple of shots in the dark too as I've played with Tk but never done any animation in it.

    1. Does the animation need to be packed in some way? That's bitten me more than once when I thought something should have shown up, it wasn't packed.
    2. You are calling MainLoop aren't you?

    HTH!

    Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.
Re: Tk::Animation won't work :(
by tphyahoo (Vicar) on May 13, 2005 at 16:04 UTC
    1) Maybe obvious but... did you
    use warnings; use strict;
    in your program?

    2) What if you just do a regular gif, not an animation, that work?

    3) What about specifying the full file path instead of just "file.gif"?

      Thanks for the reply. Alas, I always use strict and warnings, and I've tried a plain gif. Didn't work.
Re: Tk::Animation won't work :(
by davidrw (Prior) on May 13, 2005 at 16:45 UTC
    Along w/Popcorn Dave's thoughts, i also am supsicious that the widget is not being packed/displayed properly.. On thing that might be contributing (though i still couldn't get a working version after hardcoding 500's) is that $scr->width and $scr->height are both returning 1, even after the ->geomerty() call.
      You're right about the geometry thing. Let me rephrase:
      my $scr = new MainWindow; $scr->FullScreen(1); $scr->configure(-background=>"yellow"); my $canvas = $scr->Canvas( -background=>"blue", -width=>$scr->width, -height=>$scr->height )->pack( -side=>"top" ); my $tree = $canvas->Animation( -format => 'GIF', -file => 'tree.gif' )->start_animation( 20 );
      This yields a blue screen, not a yellow one, with nothing on it. I'm not sure how the pack syntax should be for Animation..? The pod remains silent on the issue...
Re: Tk::Animation won't work :(
by kwaping (Priest) on May 13, 2005 at 16:18 UTC
    This is just a shot in the dark, as I don't have Tk installed and have never used it, but did you try removing the single quotes around '-format'? I know that's exactly how it is in the perldoc for Tk::Animation, but maybe it's a typo or oversight by the author.
      Mmmmm... still won't work. Thanks though.