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

Hi

Is it forbidden to assign to the textvariable inside the validate command?

Sounds true :) maybe

Is there a way to get this done other than not using the validatecommand and binding some events (like <Return> key or focus out to some subroutine).

Um, whats wrong with using bindings or ->after?

See "phone number entry" in "Tk/demos/widget_lib/entry3.pl", sub entry3_validate_phone uses the same strategy you used, but uses ->afterIdle instead of ->after

Heh

#!/usr/bin/perl -- use strict; use warnings; use Tk; my $mw = MainWindow->new(); my $textvar = 1; my $e = $mw->Entry( -textvariable => \$textvar, -validate => 'key', ); $e->configure( -validatecommand => [ \&myValidate, $e ], ); $e->pack(); MainLoop; sub _configurevalidate_key { $Tk::widget->configure( -validate, 'key' ); } sub myValidate { my( $entry, $new, $changed, $old, $ix, $type ) = @_; #~ $entry->afterIdle( [ \&_configurevalidate_key, $e ] ); $entry->afterIdle( \&_configurevalidate_key ); 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; } __END__

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

    Hi beech

    Thanks for the reply. Generally there's nothing wrong with using bindings or after, but to me they feel like more or less ugly or cumbersome workarounds here. Usually Tk widgets have everything built in, what is required to use them. I thought that correcting an erroneous entry could be a common use case of an Entry widget, but maybe it's not.

    Anyway thanks for pointing me to afterIdle; might be a bit more robust than using after with some random timing value.

    Cheers,
    petro4213