Thanks to those who helped me with my last Tk question. Now once again I ask for help. I'm having trouble getting my widgets to change colour:
#!/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


In reply to More Perl/Tk help. 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.