in reply to Shrinking Pie Analog Countdown Timer

Here is another Tk version, :-) Mine differs in style from BrowserUk's. He made a bunch of pie segments to manipulate. I used a technique of layering an arc over a background circle, and changing their extent and color. There are many ways to do it!

UPDATE: added 'cleanup' tags to improve performance, and fixed the bell repitition problem.

#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = tkinit; $mw->fontCreate('big', -family=>'arial', -weight=>'bold', -size=>int(-18*18/14)); my $running = 0; my $canvas = $mw->Canvas(-width => 600, -height => 600, -bg =>'black')->pack(); my $start_message = $canvas->createText(300,30, -text => 'Press SpaceBar to Start', -fill => 'hotpink', -anchor => 'center', -font => 'big'); $mw->bind('<space>', \&start ); MainLoop; ########################################################### sub start{ return if $running; $running = 1; $canvas->delete('cleanup'); $canvas->itemconfigure($start_message, -state => 'hidden'); $canvas->createOval(100,100,500,500, -fill => 'white', -tags => ['backplate','cleanup'], ); my $extent = -1; my $arc = $canvas->createArc(100,100,500,500, -extent => $extent, -start => 90, -fill => 'black', -tags => ['cleanup'], ); my $repeater; $repeater = $mw->repeat(20, sub{ $extent = $extent - 1; $canvas->itemconfigure($arc, -extent => $extent); if( $extent == -320 ){ print chr(07); #beep $canvas->itemconfigure('backplate', -fill => 'red'); } if( $extent < -359 ){ $repeater->cancel; &message; } }); } ############################################## sub message{ $canvas->createText(300,300, -text => time, -anchor => 'center', -font => 'big', -tags => ['cleanup'], ); $running = 0; $canvas->itemconfigure($start_message, -state => 'normal'); }

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

Replies are listed 'Best First'.
Re^2: Shrinking Pie Analog Countdown Timer
by BrowserUk (Patriarch) on May 24, 2006 at 23:14 UTC

    There is another small problem with your version. If you modify it so that it runs as quickly as possible (set the repeat delay to 1) and to display the elapsed time from start to finish, it gets slower each time you run it?

    On my machine, the first run takes under a second, but by the 10th it takes nearly 3, and by the 20th over 5. I think this is because you are re-creating new arcs and text at each run rather than modifying the existing ones.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      It pays to have a second set of eyes looking at something. :-) Thanks for pointing out that repeated red fill update glitch.

      As far as the slowdown problem goes, it dawned on me in my dreams last night that I wasn't cleaning up those items. I checked for memory gain, but there was none, so I let it go. But after switching to delay=1, I did see some sluggish performance after about 20 runs. I added the cleanup tags, and it seems to run fine now while holding down the spacebar.

      I thought about creating the circle and arc as globals and just using hidden states to reuse them, but I thought I would show it created new in a sub, in case the tester wanted to randomly change the size and position of the circle on each run( which would be handy in a psychological test of eye movement).


      I'm not really a human, but I play one on earth. flash japh
Re^2: Shrinking Pie Analog Countdown Timer
by BrowserUk (Patriarch) on May 24, 2006 at 20:04 UTC

    I offer one modification.

    if( $extent == -320 ){

    It prevents it sounding like a fire engine on amphetamines (my console bell is rather strident). It also prevents the red extent being re-drawn 40 times and freezing (due to the bells) at 40 degrees before jumping to 0.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.