in reply to Perl/Tk without events
Since you are new to Tk and event loop systems, you probably are unaware that you cannot use "sleep" in an eventloop system, without causing problems similar to yours. Sleep interferes with the event loop.
What you want to do is use a timer, and probably a canvas, that repeatedly calls your update. Something like:
#!/usr/bin/perl use warnings; use strict; use Tk; my $top=MainWindow->new(); my $canvas=$top->Canvas(width=>300, height=>245)->pack(); $top->bind('<Control-c>' => \&exit); $top->bind('<Control-q>' => \&exit); my $origin_x=110; my $origin_y=70; my $PI=3.141592635; my $circle_radius=5; my $path_radius=0; my $angle = 0; &animate(); $top->Button(-text=> 'Again', -command=>\&animate)->pack; MainLoop(); sub animate{ $canvas->delete('circles'); $origin_x=110; $origin_y=70; $circle_radius=5; $path_radius=0; $angle = 0; my $repeater; $repeater = $top->repeat(500, sub{ #500 milliseconds $path_radius+=7; $circle_radius+=3; $angle+=10; my $path_x=$origin_x+$path_radius*cos($angle*$PI/90); my $path_y=$origin_y-$path_radius*sin($angle*$PI/90); $canvas->create('oval', $path_x-$circle_radius, $path_y-$circle_radius, $path_x+$circle_radius, $path_y+$circle_radius, -fill=>'yellow', -tags => ['circles'], ); $canvas->create('line', $origin_x, $origin_y, $path_x, $path_y, -fill=>'slategray', -tags => ['circles'], ); if( $angle > 180 ){ $repeater->cancel } } ); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl/Tk without events
by alexxxm (Acolyte) on Aug 06, 2010 at 07:34 UTC | |
by alexxxm (Acolyte) on Aug 06, 2010 at 07:52 UTC |