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

Dear Monks, I am fail to using 'sleep' inside MainLoop to animate a canvas object display on screen. Could somebody help me to solve it?
The code that i did as below:-
#!/usr/bin/perl use Tk; $top=MainWindow->new(); $canvas=$top->Canvas(width=>300, height=>245)->pack(); $origin_x=110; $origin_y=70; $PI=3.141592635; $circle_radius=5; $path_radius=0; for ($angle=0; $angle<=180; $path_radius+=7, $circle_radius+=3, $angle ++=10) { $path_x=$origin_x+$path_radius*cos($angle*$PI/90); $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'); $canvas->create('line', $origin_x, $origin_y, $path_x, $path_y, -fill=>'slategray'); sleep(1); } MainLoop();

Replies are listed 'Best First'.
Re: Animation Display appling Delay and Canvas Object
by atcroft (Abbot) on May 19, 2006 at 04:20 UTC

    If memory serves, once you pass control off to Tk's MainLoop() call, I don't think the sleep() call will make much difference. Instead, you may have to use the after() or repeat() methods documented in Tk::after. In such a case, I believe you would define a callback function that you would pass to the after() or repeat() calls, along the lines of:

    This seems to do most of what you were planning (with the exception that it loops rather than continuing outward, although that can be changed by commenting out my resetting of the circle and path radii).

    Hope that helps....

    Update 18-May-2006: Changed call method to use anonymous subroutine rather than flat list, because there is the potiential for an apparent difference in the variables' contents between the two cases (in the case of the flat list, the variables have the values they contained at the time the constructor was called; in the case of the anonymous sub, the values they contained when the anonymous sub is actually invoked).

    Update 18-May-2006: Added reference for reason for change in previous update.

    Reference: Using advanced widgets in Perl/Tk by Philipp Janert, Ph.D.

Re: Animation Display appling Delay and Canvas Object
by bbfu (Curate) on May 19, 2006 at 04:06 UTC
    You need to add a $canvas->update() before the sleep, and you'd be advised to replace the sleep with some more Tk-friendly form of delay. In fact, you probably want to replace the whole loop with a timer, whose event handler performs the necessary canvas additions. This would obviate completely the need to update as well as sleep.

    bbfu
    Black flowers blossom
    Fearless on my breath

Re: Animation Display appling Delay and Canvas Object
by zentara (Cardinal) on May 19, 2006 at 11:46 UTC
    You are experiencing some common mistakes which are encountered when trying to make animated guis. Here is your script, with just a few changes, and it does what you probably want. A few tips. Never use sleep in a gui program. Never setup for loops with delays in them. Your modified program works, but there is probably a better way to do it, like
    #!/usr/bin/perl use Tk; $top=MainWindow->new(); $canvas=$top->Canvas(width=>300, height=>245)->pack(); $origin_x=110; $origin_y=70; $PI=3.141592635; $circle_radius=5; $path_radius=0; $top->waitVisibility; for ($angle=0; $angle<=180; $path_radius+=7, $circle_radius+=3, $angle ++=10) { $path_x=$origin_x+$path_radius*cos($angle*$PI/90); $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'); $canvas->create('line', $origin_x, $origin_y, $path_x, $path_y, -fill=>'slategray'); $top->after(500); $top->update; } MainLoop();

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