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+-]+$/; };

In reply to Clearing an entry-box in TK by Panda

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.