I am once again working on my multiplication table test. My problem now is giving the user a time limit to input an answer. Please consider this code:

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


In reply to Timing events in Tk by neilwatson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.