in reply to Entry widget, default value and validatecommand

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*$.

Replies are listed 'Best First'.
Re^2: Entry widget, default value and validatecommand
by Real Perl (Beadle) on Aug 01, 2005 at 05:03 UTC

    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.