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


Dear All,
It is a memory game, I have almost completed my script. However I cannot understand, How to make Tk sleep (Please check '############# This DOES NOT WORK AS EXPECTED ' in my code)?? Here when I click on 2nd button, I want image to appear atleast for 2 secs and then disappear. However the image is disappearing when you click 2nd button. Forgive me if my explaination is Not proper.
use strict; use Tk; use Tk::Button; use Tk::Canvas; use Tk::Photo; use Tk::Splashscreen; require Tk::Splash; ##################### my @filearray; opendir( DIR, 'images' ) or die "Can't open images DIR $!"; while ( my $entry = readdir( DIR ) ) { my $type = ( -d "images\\$entry" ) ? "dir" : "file"; push @filearray, "images\\$entry" if (($type eq "file") && ($e +ntry =~ /\.gif$/)); #print "$type\t$entry\t@filearray\n"; } closedir( DIR ); @filearray = (@filearray,@filearray); #################### #fisher_yates_shuffle(\@filearray); unshift @filearray, "NIL"; #foreach (@filearray) {print $_,"\n";}; my @arranged_gifs; my $top = new MainWindow; my $loop_cntr=0; my $state_of_button=-1; my $prev_x = undef; my $prev_y = undef; my $number_of_clicks=0; my $prev_clicked_button=undef; foreach my $x (1..10) { for my $y (1..5) { $loop_cntr++; $arranged_gifs[$x][$y]= $filearray[$loop_cntr]; #print "$loop_cntr:$x:$y:-".$arranged_gifs[$x][$y]."\t".$filea +rray[$loop_cntr]."\n"; my $button = $top->Button(-width => 10, -height =>3,-command=> +sub{click_button($x,$y,\$top->gridSlaves(-row=>$x,-column=>$y), \$pre +v_clicked_button)}); $button->configure(-text=>'Hey'); $button->grid(-row =>$x , -column =>$y); } } sub click_button($$$){ my ($myx,$myy,$clicked_button,$prev_clicked_button)=@_; $number_of_clicks++; my $mywait; my $image = $top->Photo(-format => 'gif' , -file=>$arranged_gifs[$ +myx][$myy]); ${$clicked_button}->configure(-width => 50, -height =>30, -image=> +$image, -state=>'disabled'); if ($number_of_clicks == 1) { print "Hello\n"; ${$prev_clicked_button} = ${$clicked_button}; } if ($number_of_clicks == 2) { print "pair\n"; ############# This DOES NOT WORK AS EXPECTED sleep(2); ##################################### $number_of_clicks=0; ${$prev_clicked_button}->configure(-width => 10, -height =>3, +-state=>'normal', -image=>undef); ${$clicked_button}->configure(-width => 10, -height =>3, -stat +e=>'normal', -image=>undef); } } sub fisher_yates_shuffle { my $array = shift; my $i; for ($i = @$array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @$array[$i,$j] = @$array[$j,$i]; } } MainLoop;
Thanks,
SanPerl

Replies are listed 'Best First'.
Re: Tk- Image Flashing for few seconds
by shmem (Chancellor) on Nov 24, 2006 at 11:54 UTC
    From what I remember of Tk (some time now) - don't use sleep, since that messes with Tk internals. Use Tk::after (the manual page should be in the Tk docs), something like
    ${$prev_clicked_button} -> after (2000, [\&hide, ${$prev_clicked_b +utton} ]); ${$clicked_button} -> after (2000, [\&hide, ${$clicked_button +}]);
    sub hide { shift -> configure (-image => undef); }

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Great this solved my problem
      SanPerl