in reply to Tk Canvas Animation

That loop construct you have will not work. Tk will block until your sub returns. That is why there is "Tk::after - Execute a command after a time delay."

your code modified (to do somewhat what you want, some numbers may be off, but it's animated ;D)

#!/usr/bin/perl -w use strict; use Tk; sub jump_it { my ($c, $thing) = @_; my ($old_x, $old_y) = $c->coords($thing); my $new_pos = $old_x == 10 ? 80 : 10; $c->coords($thing, $new_pos, $new_pos); } =head1 ORIGINAL C<slide_it> function sub slide_it { my ($c, $thing) = @_; for my $i (10..80) { $c->coords($thing, $i, $i); for (my $wait=20000; --$wait;){}; } } =cut sub slide_it # canvas, card { my ($c, $thing) = @_; $$c{__slide_for} = 80; $$c{__slide_count} = 0; $$c{__slide_timer} = $c->repeat(100, # miliseconds [ \&move_thing, $c, $thing, 2, # this many pixels X 2, # this many pixels Y ,] ,); } sub move_thing { my($c, $thing, $nX, $nY) = @_; if( $$c{__slide_count} > $$c{__slide_for} ) { $$c{__slide_timer}->cancel() } $$c{__slide_count}++; $c->move( $thing, #what? $nX, $nY, ,); } my $mw = MainWindow->new; my $c = $mw->Canvas(-relief => 'sunken', -bd => 2, -width => 200, -height => 200); my $image = $c->Photo(-file => "cards/ah.gif"); my $card = $c->create('image', 10, 10, -anchor => 'nw', -image => $image); my $j_btn = $mw->Button(-text => 'Jump It', -command => [\&jump_it, $c, $card] ); my $s_btn = $mw->Button(-text => 'Slide It', -command => [\&slide_it, $c, $card] ); $j_btn->pack; $s_btn->pack; $c->pack; MainLoop;
update:Instead of :
my $image = $c->Photo(-file => "cards/ah.gif"); my $card = $c->create('image', 10, 10, -anchor => 'nw', -image => $image);
you're probably better off with
my $image = $c->Photo(-file => "cards/ah.gif"); $$image{_id} = $c->create('image', 10, 10, -anchor => 'nw', -image => $image);
That way when you implement a timer, you can store it in $$image{_timer} .... since you're going to have a different Photo object for each card (for the most part) ... anyway, not real important to your question (I'd probably have @cards which would hold about 52 Tk::Photo objects ....)

update: ++danger though I like my way more ;)

 
______crazyinsomniac_____________________________
Of all the things I've lost, I miss my mind the most.
perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Replies are listed 'Best First'.
Re: (crazyinsomniac) Re: Tk Canvas Animation
by danger (Priest) on Jan 02, 2002 at 14:17 UTC

    Alternatively, you can use the update() method within the original callback function (and set a delay with the after() method if desired):

    sub slide_it { my ($c, $thing) = @_; for my $i (10..80) { $c->coords($thing, $i, $i); $c->update; # update $c widget # $c->after(100); # delay if desired } }