Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^2: Entry Widget - validatecommand

by shortyfw06 (Beadle)
on Jul 23, 2012 at 16:58 UTC ( #983205=note: print w/replies, xml ) Need Help??

in reply to Re: Entry Widget - validatecommand
in thread Entry Widget - validatecommand

Thank you. I've tried this code and it is allowing inputs other than numbers (positive or negative). I'd like to limit the entry options to only positive or negative integers.

Replies are listed 'Best First'.
Re^3: Entry Widget - validatecommand
by Athanasius (Chancellor) on Jul 24, 2012 at 02:23 UTC
    this code ... is allowing inputs other than numbers

    Not so! For the record, here is the exact code I am running (using Strawberry perl v5.16.0 built for MSWin32-x86-multi-thread-64int, running under Windows Vista 32-bit, and with Tk 804.030):

    #! perl use strict; use warnings; use Tk; my $value; my $mw = new MainWindow; my $ent = $mw->Entry(-textvariable => \$value, -validate => 'key', -validatecommand => sub { $_[0] =~ /^(?:|-|\d+|-\ +d+)$/ }, -invalidcommand => \&lam_num_error)->pack(); my $print_button = $mw->Button(-text => "Print", -command => \&printx, -font => "ansi 10 bold")->pack(); my $reset_frm = $mw->Frame(); $reset_frm->pack(-fill => 'both'); my $reset_button = $reset_frm->Button(-text => "Reset", -command => \&do_reset, -font => "ansi 10 bold")->pac +k(); MainLoop; sub printx { print $value unless $value eq '-'; } sub do_reset { $ent->delete(0, 'end'); } sub lam_num_error { $mw->messageBox(-message => "The input must be an integer."); } __END__

    This lets me enter -42, 763, etc., but if I try to enter a letter, or a punctuation symbol, or a minus sign anywhere after the first character, or even a space, I immediately get the message “The input must be an integer.” and my input never makes it into the Entry box. In other words, the code is working exactly as required.

    Athanasius <°(((><contra mundum

      I apologize Athanasius. It does do exactly what you say. Thank you very much for being so helpful! :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://983205]
[ambrus]: I hope Corion or some other admin is here and can check the logs to see what the problem is.
[mz2255]: I wish I had an online shop but sadly no. The title field definitly wasn't short, had a perl module in the title with 5-6 additional words.
[ambrus]: you can also try to just post again in case it was some intermittent error
[mz2255]: Tried again, denied. Maybe my code is just so bad the site refuses to post it.
[marto]: no, there's a bug, sometimes...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2017-10-19 15:36 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (255 votes). Check out past polls.