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

In my spare time, I put together a little game (see below). The game seems to work perfectly except for the fact that after entering a number and pushing the 'enter' button, the user needs to clear the entry-box.

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

    You could work with -textvariable (instead of -text) which binds a reference to a variable to the content of the entry, e.g

    use vars qw($InputText); $InputText = ""; ... my $input = $mw->Entry (-textvariable => \$InputText)->form; ... # my $guess = int ($input->get); my $guess = int( $InputText ); $InputText = ""; # empty it

    Another Idea: If you just want to allow integer values to be entered, it often is a good way not to allow other input than numbers which makes errorchecking easier. Maybe Tk::NumEntry could be interesting for you, or my Tk::EntryCheck, e.g.

    my $input = $mw->EntryCheck( -textvariable => \$InputText, -pattern => qr~\d~, )->form;

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

      Thanks strat, I'll get GrandFather to download Tk::EntryCheck so I can try the game using it as well.
Re: Clearing and entry-box in TK
by zentara (Cardinal) on Sep 23, 2006 at 11:48 UTC
    Hi, you don't need the Enter button bar to enter the data. Use
    my $input = $mw->Entry (-text => "",)->form; $input->focus; $input->bind('<Return>', \&enter);
    that will put the cursor right into the entry widget, so the player can start typing numbers, followed by the Enter key to play the number.

    To clear the last number out, put this at the end of your sub enter:

    sub enter{ ...... ..... $input->delete(0,'end'); }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Thanks zentara, I've tried using that in my code and it works perfectly.