neilwatson has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use warnings; use strict; use Tk; #flashcards for multiplication tables #Neil H watson #Tue Jul 15 20:24:47 EDT 2003 #upper time table limit my $scale; my $table; my ($score, $guess, $num1, $num2, $question, $answer); my ($correct, $count, $percent, $rightwrong); my $colour = 'purple'; #define main window my $mw = MainWindow->new; #create a large readable font my $font = $mw->fontCreate('font', -family => 'courier', -size => 28, -weight => 'bold'); $mw->title("Times Tables"); #return key prints question without #calculation the first score $mw->bind("<KP_Enter>", \&question); #scale to select upper limit (up to 12) $mw->Scale(-from => 1, -to => 12, -variable => \$scale)->pack(-side => 'left'); #guess widget $mw->Entry(-width => 3, -takefocus => 1, -font => $font, -textvariable => \$guess)->pack(-side => 'right'); #question widget $mw->Label(-textvariable => \$question, -font => $font)->pack(-side => 'right'); #right/wrong widget $mw->Label(-textvariable => \$rightwrong, -foreground => $colour)->pack(-side=> 'bottom'); #score widget $mw->Label(-textvariable => \$score)->pack(-side => 'bottom'); MainLoop; #checking sub check { if ($answer == $guess) { #count correct answers $correct++; $rightwrong = 'Correct!'; $colour = 'green'; }else{ $rightwrong = 'Wrong!'; $colour = 'red'; } #count questions $count++; #calculate score $percent = int($correct/$count*100); $score = "$correct correct out of $count\n($percent%)"; #clear guess $guess =""; question(); } sub question { #generate question and answer $table = $scale; $num1 = int(rand($table)+1); $num2 = int(rand(12)+1); $answer = $num1*$num2; $question = "$num1 x $num2 = "; #return checks answer and prints #new question. This prevents #the first loop from calculating #a score $mw->bind("<KP_Enter>", \&check); }
After each question, whether right or wrong, the colour of the right/wrong widget never changes. Why?
Neil Watson
watson-wilson.ca
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: More Perl/Tk help.
by fergal (Chaplain) on Jul 27, 2003 at 19:35 UTC | |
|
Re: More Perl/Tk help.
by Aristotle (Chancellor) on Jul 27, 2003 at 19:25 UTC | |
|
Re: More Perl/Tk help.
by neilwatson (Priest) on Jul 27, 2003 at 20:25 UTC |