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

Fellow monks,
I am delving into Tk in an attempt to teach a child multiplication tables. My idea was:
  1. Use a scale from 1 to 12 to select times tables being tested.
  2. Generate question and answer box.
  3. Get answer.
  4. Check answer.
  5. Compute score.
  6. Regenerate question and answer box...

I am not familiar with the 'flow' of widgets and windows which makes it hard for me to generate code. Could some kind monks please help me? Here is what I have so far:

#!/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, $answer); #define main window my $mw = MainWindow->new; $mw->title("Times Table"); #scale to select upper limit (up to 12) $mw->Scale(-from => 1, -to => 12, -variable => \$scale)->pack(-side => 'left'); question(); #guess widget $mw->Entry(-width => 3, -takefocus => 1, -textvariable => \$guess)->pack(-side => 'right'); #question widget $mw->Label(-text => "$num1 X $num2 =")->pack(-side => 'right'); #checking if ($answer == $guess) { $score++ }else{ $score-- } MainLoop; sub question { $table = $scale->get(); $num1 = int(rand($table)+1); $num2 = int(rand(12)+1); $answer = $num1*$num2; }

It doesn't do alot at this point (very rough code). Here are my questions:

  1. The get is not working. How do I get the value of the scale at any given time?
  2. After the user enters and answer and hits enter, how do I get the window to checks answers, compute scores and generate a new question?

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Newbie Perl/Tk help
by Mr. Muskrat (Canon) on Jul 17, 2003 at 15:41 UTC

    1. $table = $scale;

    2. Couple things to do here.

    .... my ($score, $guess, $num1, $num2, $answer, $question); # added $questi +on .... #question widget $mw->Label(-textvariable => \$question)->pack(-side => 'right'); # cha +nged -text to -textvariable # button to submit answer $mw->Button(-text => 'Check me', -command => sub { checkanswer(); } )- +>pack(-side => 'right'); # add you score display stuff too .... sub question { $table = $scale; $num1 = int(rand($table)+1); $num2 = int(rand(12)+1); $answer = $num1*$num2; $question = "$num1 x $num2 = "; } sub checkanswer { if ($answer == $guess) { $score++ }else{ $score-- } # update score display here question(); # <-- generate a new question }

Re: Newbie Perl/Tk help
by cbro (Pilgrim) on Jul 17, 2003 at 15:40 UTC
    Here is a small example that hints answers towards both of your questions.
    #!c:/perl/bin use Tk; use strict; my $main = MainWindow->new(); my $value; # # here is an example of how to catch the key return. # Again, this is just an example, but you will # see that you get into the calc() function every time # the user presses enter. $main->bind('<Key-Return>', \&calc); # O/K now that the Enter key is bound to the # window, let's bind $value to the Scale and # also give Scale a function where we can access # it's members (e.g. the value of $value) $main->Scale('orient' => 'horizontal', '-from' => 0, '-to' => 12, '-tickinterval' => 1, '-label' => 'Select Time Table', '-variable' => \$value, '-command' => \&calc)->pack; MainLoop; sub calc { # remember this is just an example. # This prints to the console, but # it shows that you have access to the value. print $value . "\n"; # # Now that you're in the function, I'll leave # it up to you how to do the calculations, and # draw up/print a new question. # just know that you still have access to all of the # widgets that are part of the MainWindow. }