in reply to Tk validateCommand Help

Tk has no standard for the first element being passed to a sub. Only the bind method will pass itself as a widget reference as $_[0] by default. So you have to work out your own system. You have a couple of options. You can pass it like davidrw showed you, or you can use a different syntax like
-validatecommand => [\&Validate_Number, $entry_limit]
You can also use the caller method to get which widget called the sub like in the example below. You can use the caller in conjunction with hashes, to do some complex switching. You could put each entry's specifications into a hoh like
my %entry =( 1=>{ 'object' =>undef, 'upperlim => 42, 'lowerlim =>0 }, 2=>{ 'object' =>undef, 'upperlim => 99, 'lowerlim =>-23 }, );
Then in your validate callback, just pass the entry number like
-validatecommand => [\&Validate_Number, 1]

Here is an example using button to demonstrate the caller function.

#!/usr/bin/perl use strict; use Tk; my $mw = MainWindow->new; for(0..4){ $mw->Button(-text => "Hello World$_", -command=>[\&change])->pack; } MainLoop; sub change { print "sub-input->@_\n"; my $caller = $Tk::widget; print "$caller "; print $caller->{'_TkValue_'},' '; my $text = $caller->cget('-text'); print "$text\n"; $caller->configure(-text=>"Hello Stranger"); }

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re^2: Tk validateCommand Help
by NateTut (Deacon) on Jun 07, 2005 at 13:02 UTC
    Thanks to both of you for your excellent advice.