CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Tk Entry –validatecommand



May 13, 2014, 8:55 AM

Post #1 of 3 (9039 views)
Tk Entry –validatecommand Can't Post

Tk Entry –validatecommand

I can’t get the validatecommand option to work in a Tk Entry widget. Any suggestions?

use Modern::Perl '2013'; 
use Tk;
my $window = Tk::MainWindow->new(-width => 300, -height => 300,);

my $amountStr1 = '$123.45';
my $amountStr2 = '$678.90';

$window->Entry ( -justify => 'right',
-textvariable => $amountStr1,
-validate => 'focusout',
-validatecommand => [\&AmountChanged, 1, 2],
-width => 15 )->pack;
$window->Entry ( -justify => 'right',
-textvariable => $amountStr2,
-validate => 'focusout',
-validatecommand => [\&AmountChanged, 3, 4],
-width => 15 )->pack;

sub AmountChanged
my ($column, $row) = @_;
print ("amounts changed to ( $amountStr1 , $amountStr2 ) at $column x $row\n");
return 0;

This code will display two entry widgets. As I understood the manual pages, if you place your cursor on the one of the widgets and change its value, then place the cursor on the other widget, this should cause a “focusout” condition and call the AmountChanged sub. This is not happening, AmountChanged is only called when the widgets are first created.
Any suggestions?


Veteran / Moderator

May 13, 2014, 9:22 AM

Post #2 of 3 (9025 views)
Re: [PapaGeek] Tk Entry –validatecommand [In reply to] Can't Post

2 problems

1) When assigning -textvariable, you need to use/pass a reference.

-textvariable => \$amountStr1,

2) The return 0; statement is resetting -validate to its default value of none. I'm not sure why it's doing that, but if you return 1 instead of 0, you'll get your expected results.


May 13, 2014, 10:10 AM

Post #3 of 3 (9005 views)
Re: [FishMonger] Tk Entry –validatecommand [In reply to] Can't Post

Thank you, those changes did work. I'm guessing that the first call, during creation, sets to widget as "invalid" when it returned 0, and after that it just didn't call the changed sub again!

Anyway, thanks again, your post did fix the issue!


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives