in reply to Tk::Entry widget with textvariable and validatecommand

G'day petro4213,

Welcome to the Monastery.

Without any code, it's unclear what you're actually attempting and how it's not doing what you want. Please read "How do I post a question effectively?". Then provide a "SSCCE": keep it short; do not include cosmetic elements (e.g. colours, fonts, etc.); include a small piece of ASCII art to describe your GUI where appropriate (although, there's probably no need here).

Read the documentation for Tk::Entry, especially the VALIDATION section.

— Ken

  • Comment on Re: Tk::Entry widget with textvariable and validatecommand

Replies are listed 'Best First'.
Re^2: Tk::Entry widget with textvariable and validatecommand
by petro4213 (Acolyte) on Nov 06, 2017 at 18:21 UTC

    Hi Ken

    thanks for replying. As my question seemed so general and simple to me, I thought I could go without any code. Of course I read the docu first, but it only says that using textvariable and validatecommand "can be dangerous to mix", which doesn't help much. Anyway, let's try with this code snippet:

    use Tk; my $mw = MainWindow->new(); my $textvar = 1; $mw->Entry(-textvariable => \$textvar, -validatecommand => sub { my ($new,$changed,$old,$ix,$type) = @_; return 1 if (!defined($changed)); return 1 if ($new eq "") or ($type<0); $textvar = $new & 3; # use only 2 low bits print "masked out entry value\n"; return 1; }, -validate => 'key')->pack(); MainLoop;

    When started, a window with an Entry widget appears, with value 1 displayed. If you first enter e.g. the value 7, it will print the message and change the value to 3 (because 7 & 3 = 3). Any further entries will pass unchanged and no message will be printed anymore.

    Therefore I assume, that the validate option was set to 'none'. The docu says, that this happens, when some error occurred (maybe setting the textvar from the validatecommand callback is such an error?).

    Does this clarify my problem? Do you have any advice, better than my ugly workaround of using an "after" call

    Thanks and kind regards
    petro4213