Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Animation using Tkx

by narayan.1979 (Initiate)
on Nov 27, 2009 at 05:45 UTC ( #809693=perlquestion: print w/replies, xml ) Need Help??

narayan.1979 has asked for the wisdom of the Perl Monks concerning the following question:


I am able to use the Tk::Animation and animate a gif. But I want to do the same usin Tkx as my entire GUI is already built on Tkx. I could not find any example of information fo using animation using Tkx. Can someone help me.

I have pasted the simple code i used to animate using Tk.

use Tk; use Tk::Animation; my $mw = MainWindow->new; $mw->title("Hello World"); $mw->Photo('C:/Documents and Settings/narayang/Desktop/progress.gif', +-file => 'C:/Documents and Settings/narayang/Desktop/progress.gif'); my $animate; if (@ARGV) { $animate = $mw->Animation; foreach (@ARGV) { $animate->add_frame($mw->Photo(-file => $_)); } } else { } my $gif89 = Tk->findINC('Gouraud_low_anim.gif'); $animate = $mw->Animation(-format => 'gif', -file => 'C:/Documents and + Settings/narayang/Desktop/progress.gif'); $animate->set_image(0); my $lab = $mw->Label(-text => "asd",-image => $animate)->pack; my $start = $mw->Button( -text => 'Start', -command => [$animate => 'start_animation', 100])->pack; my $stop = $mw->Button( -text => 'Stop', -command => [$animate => 'stop_animation'])->pack; MainLoop; 1;

Replies are listed 'Best First'.
Re: Animation using Tkx
by Kirsle (Pilgrim) on Nov 27, 2009 at 21:39 UTC

    Try looking into the source code of Tk::Animation.

    It looks like it creates a new Tk::Photo object for each index of the image, using `-format "GIF -index $index"`:

    sub new { my ($class,$widget,%args) = @_; my $obj = $class->SUPER::new($widget,%args); $obj->{'_MainWIndow_'} = $widget->MainWindow; if ($args{'-format'} eq 'gif') { my @images; local $@; while (1) { my $index = @images; $args{'-format'} = "gif -index $index"; my $img; eval {local $SIG{'__DIE__'}; $img = $class->SUPER::new($widget,% +args) }; last if $@; push(@images,$img); } if (@images > 1) { $obj->add_frame(@images); $obj->{'_frame_index_'} = 0; } } $obj->set_image( 0 ); $obj->_get_gif_info; return $obj; }

    Tk::Animation basically just creates a ton of Photo objects, and a label, and then with a $tk->repeat loop, it updates the label with the next image in the animation.

    sub start_animation { my ($obj,$period) = @_; my $frames = $obj->{'_frames_'}; return unless $frames && @$frames; my $w = $obj->MainWindow; $obj->stop_animation; $obj->{'_period_'} = $period if $period; $obj->{'_NextId_'} = $w->repeat($obj->{'_period_'},[$obj,'next_image_ +in_animation']); }

    It should be trivial to re-implement this module in Tkx. If Tkx doesn't have a "-index" option in the image's -format you may need to use an external module like Imager to split up the frames of the gif, and make a new Photo object or similar for each frame.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://809693]
Front-paged by keszler
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2021-10-21 10:40 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (83 votes). Check out past polls.