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.

In reply to Re: Tk::Entry validate question by graff
in thread Tk::Entry validate question by Baboon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.