Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Tk::Entry validate question

by graff (Chancellor)
on Jun 16, 2002 at 04:54 UTC ( [id://174899]=note: print w/replies, xml ) Need Help??


in reply to Tk::Entry validate question

Maybe it would be appropriate to use two different variables; one for the Entry "-textvariable", and another for the Scale "-variable"; then, use callbacks to keep the two in sync -- something like this:
#!/usr/bin/perl -w use strict; use Tk; my $scale_var = 0; my $entry_var; my $main = MainWindow->new(); my $entry = $main->Entry(-textvariable => \$entry_var, )->pack(); my $scale = $main->Scale(-variable => \$scale_var, -length => '100p', -from => 0, -to => 100, -command => sub { $entry_var = $scale_var }, )->pack(); $entry->bind( '<Return>', sub { if ( $entry_var =~ /^\d+$/ && $entry_var <= 100 ) { $scale_var = $entry_var; } else { print STDOUT "\a\a"; $entry->delete( 0, 'end' ); } } ); MainLoop;
Here, the "-command" callback of the Scale widget will update the "-textvariable" of the Entry widget whenever the Scale is adjusted manually -- works just as well as using the same variable for both widgets.

When the user decides to type a value into the Entry to control the Scale position, this approach requires that the "<Return>" key be used to signal that the user is done editing the Entry content and the string value is ready for application to the Scale variable. The binding of the "<Return>" event in the Entry widget will handle the validation -- checking not only that it is all digits, but also that the (integer) number value falls within the min-max range for the Scale widget.

Personally, I tend to prefer this sort of approach for using strings in an Entry -- let users make as many edit operations as they want, of whatever sort, so long as they issue an explicit event once they decide that the string value is ready for use. Tk::Entry's "validate" mechanism does not support this sort of trigger -- I suppose because it would be redundant, since "bind" does this perfectly well.

update: I know a lot of people do prefer instant feedback, so instead of binding on the "<Return>" event, one could instead bind on "<KeyRelease>", and change (simplify) the callback slightly:

$entry->bind( '<KeyRelease>', sub { if ( $entry_var =~ /^\d+$/ && $entry_var <= 100 ) { $scale_var = $entry_var; } } );
In this case, the Entry variable is simply ignored if it is empty or contains invalid data; otherwise, it is applied to the Scale variable as soon as each valid character is entered each time a KeyRelease event leaves a valid string in the Entry variable.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://174899]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-28 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found