neilwatson has asked for the wisdom of the Perl Monks concerning the following question:
18 #time limit per question 19 my $limit = 10; 20 21 #define main window 22 my $mw = MainWindow->new; 23 24 #create a large readable font 25 my $font = $mw->fontCreate('font', 26 -family => 'courier', 27 -size => 28, 28 -weight => 'bold'); 29 30 $mw->title("Times Tables"); 31 32 #return key prints question without 33 #calculation the first score 34 $mw->bind("<KP_Enter>", \&question); 35 36 #scale to select upper limit (up to 12) 37 $mw->Scale(-from => 1, 38 -to => 12, 39 -variable => \$scale)->pack(-side => 'left'); 40 41 #guess widget 42 $mw->Entry(-width => 3, 43 -takefocus => 1, 44 -font => $font, 45 -textvariable => \$guess)->pack(-side => 'right'); 46 47 #question widget 48 $mw->Label(-textvariable => \$question, 49 -font => $font)->pack(-side => 'right'); 50 51 #right/wrong widget 52 my $rw = $mw->Label(-textvariable => \$rightwrong, 53 -foreground => $colour)->pack(-side=> 'bottom'); 54 55 #score widget 56 $mw->Label(-textvariable => \$score)->pack(-side => 'right'); 57 58 #timer for answer 59 if ($count >= 1){ 60 $timer = $mw->repeat($limit, \&check); 61 } 62 63 64 MainLoop; 65 66 #checking 67 sub check { 68 if ($count >= 1){ 69 $timer->cancel(); 70 } 71 72 if ($answer == $guess) { 73 #count correct answers 74 $correct++; 75 $rightwrong = 'Correct!'; 76 $colour = 'dark green'; 77 }else{ 78 $rightwrong = "Wrong! $num1 x $num2 = $answer"; 79 $colour = 'red'; 80 } 81 $rw->configure(foreground => $colour); 82 83 #count questions 84 $count++; 85 86 #calculate score 87 $percent = int($correct/$count*100); 88 $score = "$correct correct out of $count\n($percent%)"; 89 90 #clear guess 91 $guess = ""; 92 93 question(); 94 } 95 96 97 sub question { 98 #generate question and answer 99 $table = $scale; 100 $num1 = int(rand($table)+1); 101 $num2 = int(rand(12)+1); 102 $answer = $num1*$num2; 103 $question = "$num1 x $num2 = "; 104 105 #return checks answer and prints 106 #new question. This prevents 107 #the first loop from calculating 108 #a score 109 $mw->bind("<KP_Enter>", \&check); 110 }
How can I have the program check the answer automatically after 10 seconds regardless of user input?
Neil Watson
watson-wilson.ca
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Timing events in Tk
by tedrek (Pilgrim) on Aug 07, 2003 at 00:46 UTC | |
by neilwatson (Priest) on Aug 09, 2003 at 12:54 UTC | |
by tedrek (Pilgrim) on Aug 10, 2003 at 01:54 UTC | |
|
Re: Timing events in Tk
by waswas-fng (Curate) on Aug 07, 2003 at 00:13 UTC |