Panda has asked for the wisdom of the Perl Monks concerning the following question:
I thought that putting a bit of code in the 'enter' subroutine and the 'newgame' subroutine that clears the box after the 'enter' or 'newgame' button is pushed would be useful, but I can't figure out how to do it.
I would really appreciate if if someone could show me how.
use warnings; use strict; use Tk; my $mw = new MainWindow (-title => "Panda's Game"); my $rules = $mw->Label (-text => "This game is easy. I'm thinking of a + number between 0 and 1001 (but not including them) and you have to g +uess it. You get fifteen guesses, just type in your guess in the entry box and +I will tell you if it is smaller or larger than my number.",)->form; my $input = $mw->Entry (-text => "",)->form; my $output = $mw->Label (-text => "",)->form; my $enterButton = $mw->Button (-text => "Enter", -command => \&enter)- +>form; my $quitButton = $mw->Button (-text => "Quit", -command => sub{exit} ) +->form; my $newGameButton = $mw->Button (-text => "New Game", -command => \&ne +wGame)->form; $rules->form (-left => '%0', -right => '%100', -top => '%0', -bottom = +> '%40',); $input->form (-left => '%40', -right => '%60', -bottom => '%100', -top + => '%80',); $output->form (-left => '%0', -right => '%100', -bottom => '%60', -top + => '%40',); $enterButton->form (-left => '%0', -right => '%100', -bottom => '%80', + -top => '%60',); $quitButton->form (-right => '%40', -left => '%0', -bottom => '%100', +-top => '%80',); $newGameButton->form (-right => '%100', -left => '%60', -bottom => '%1 +00', -top => '%80',); $mw->geometry('600x200'); my $answer = int (rand (1000) + 1); my $counter = 0; MainLoop; sub newGame { $answer = int (rand (1000) + 1); $counter = 0; $output->configure (-text => "New Game Started"); }; sub enter { no warnings; my $guess = int ($input->get); if ($guess == $answer) { $output->configure (-text => "Correct! My number is $answer. P +ress 'Quit' to exit or 'New Game' to play again.") } elsif (! isNumber ($guess) or ($guess < 1 or $guess >1000)) { $output->configure (-text => "That is either not a number or i +s not within 0 and 1001!"); } elsif($guess < $answer) { $output->configure (-text => "My number is bigger than $guess. +"); } elsif($guess > $answer) { $output->configure (-text => "My number is smaller than $guess +."); } ; $counter = ($counter + 1); if ($counter > 14) { $output->configure (-text => "Time up! My number was $answer. Pres +s 'Quit' to exit or 'New Game' to play again.") } }; sub isNumber { my $value = shift; return $value =~ /^[\d+-]+$/; };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Clearing and entry-box in TK
by strat (Canon) on Sep 23, 2006 at 10:08 UTC | |
by Panda (Scribe) on Sep 24, 2006 at 01:57 UTC | |
|
Re: Clearing and entry-box in TK
by zentara (Cardinal) on Sep 23, 2006 at 11:48 UTC | |
by Panda (Scribe) on Sep 24, 2006 at 01:53 UTC |