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

Dear Monks,

I would like to know if there is way for me to restrict my user from what he can enter in an entry widget, but at the same time have a default value in that widget as the application loads. The code below does not display the value in the entry widget:
#!/usr/bin/perl -w use strict; use Tk; my $page2; my $w=0; my $wtf; my $wentry= 3; my $wcb; $page2 = MainWindow -> new(); $wcb = $page2 ->Checkbutton (-text=> '/W:', -variable=> \$w, -font=>"A +dobe 10")->pack; #$wcb->select(); $wtf = $page2 ->Entry (-textvariable=> \ $wentry, -width=>8, -validate=> 'key', -validatecommand=> sub{$_[1] =~/[0-9]/}, -invalidcommand=> sub{$page2->bell} )->pack; MainLoop;

However, if I comment out the

-validatecommand=> sub{$_[1] =~/[0-9]/},
line, it works great.

Thanks in advance for any idea about how I could have the best of both world. -- I would like to not have to check what crap the user can give me after the fact.

Claire

Replies are listed 'Best First'.
Re: Entry widget, default value and validatecommand
by pg (Canon) on Aug 01, 2005 at 03:12 UTC

    If you only allow the user to enter a single digit number, then change your code to:

    use strict; use Tk; my $page2; my $w=0; my $wtf; my $wentry= 3; my $wcb; $page2 = MainWindow -> new(); $wcb = $page2 ->Checkbutton (-text=> '/W:', -variable=> \$w, -font=>"A +dobe 10")->pack; #$wcb->select(); $wtf = $page2 ->Entry (-textvariable=> \ $wentry, -width=>8, -validate=> 'key', -validatecommand=> sub{$_[0] =~ /^\d{0,1}$/}, -invalidcommand=> sub{$page2->bell} )->pack; MainLoop;

    You have to allow blank, otherwise the user cannot clear the entry before entering.

    If you allow multi-digit numbers, change the regexp to ^\d*$.

      Thank you so much!

      I modified your code to read -validatecommand=> sub{$_[0] =~ /^\d*\${0,1}$/}, so my user can enter as many digits as he wants, but still only digits and the default value shows up!

      I Love it!

        Note that there is a subtle difference between this code and your original code. Your original code was validating new characters as they were added (and characters being deleted). This code validates the whole string, which I suspect is more likely what you wanted anyway. This way you can easily add range validation and stuff too.


        Perl is Huffman encoded by design.
Re: Entry widget, default value and validatecommand
by GrandFather (Saint) on Aug 01, 2005 at 02:23 UTC

    Checking for defined $_[1] may help.

    From the docs describing the second parameter in the callback:

    The characters to be added (or deleted). This will be undef if validation is due to focus, explcit call to validate or if change is due to -textvariable changing.

    Update: add explanation

    Perl is Huffman encoded by design.
Re: Entry widget, default value and validatecommand
by strat (Canon) on Aug 01, 2005 at 06:35 UTC

    See the code of CPAN: Tk::EntryCheck for an example

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