Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi perlingRod,

You're getting a double callback because of this line in your code:

$f2 -> focus;

Don't do that; it's triggering a focusout event on $f2, causing $f2 to enter its validation callback.

Note too, the first time you validate on $entryOne or $entryThree the corresponding column 2 Entry widget $entryTwo or $entryFour will not have been selectable, so the focus will skip to the other first-column Entry widget. If this isn't what you want, you could change the disabled states to readonly instead, because according to the Entry widget docs:

Switch: -state ... If the entry is readonly, then the value may not be changed using +widget commands and no insertion cursor will be displayed, even if th +e input focus is in the widget; the contents of the widget may still +be selected. If the entry is disabled, the value may not be changed, +no insertion cursor will be displayed, the contents will not be selec +table, ...

Finally, a suggestion (which you are free to take as you will). I find it useful to simplify Tk code by writing subroutines that consolidate common values. Since you're creating 4 Entry widgets with the same validation method, and your validation calls the same 2 subroutine, why not extrapolate that to the 2 new subroutines (here called entry and config_validate):

#!C:\Windows\Config\UMS_Scripts\perl\bin\perl use strict; use warnings; use Tk; my $globalVar = 0; ################# ## Subroutines ## ################# sub first { my ($f1, $f2) = @_; $globalVar = $globalVar + 1; print "in sub first for the $globalVar time\n"; my $count = 1; for my $anArg (@_) { if (defined $anArg) { print "$count - $anArg\n"; } else { print "$count\n"; } $count = $count + 1; } my $value1 = $f1 -> get(); my $length1 = length $value1; print "value1: $value1 :$length1\n"; my $value2 = $f2 -> get(); my $length2 = length $value2; print "value2: $value2 :$length2\n"; if ($length1 > 0) { $f2 -> configure(-state=>'normal'); ## Don't do this -- it invoke the focusout event prematurely # $f2 -> focus; # $f2 -> update; } else { } } sub second { my ($f1, $f2) = @_; print "in sub second\n"; ## You probably don't need this -- unused in this subroutine # $globalVar = $globalVar + 1; my $count = 1; for my $anArg (@_) { if (defined $anArg) { print "$count - $anArg\n"; } else { print "$count\n"; } $count = $count + 1; } my $value1 = $f1 -> get(); my $length1 = length $value1; print "value1: $value1 :$length1\n"; my $value2 = $f2 -> get(); my $length2 = length $value2; print "value2: $value2 :$length2\n"; } ################## ## Main Program ## ################## my $Mw = MainWindow->new(-title=>'Collecting Passwords'); my $entryOne = entry($Mw, 0, 0, "normal"); my $entryTwo = entry($Mw, 0, 1, "disabled"); my $entryThree = entry($Mw, 1, 0, "normal"); my $entryFour = entry($Mw, 1, 1, "disabled"); config_validate($entryOne, $entryTwo); config_validate($entryThree, $entryFour); MainLoop; ##################### ## New Subroutines ## ##################### sub entry { my ($parent, $row, $col, $state) = @_; my $entry = $parent->Entry(-validate => 'focusout'); $entry->grid(-row => $row, -column => $col); $entry->configure(-state => $state); return $entry; } sub config_validate { my ($ent_A, $ent_B) = @_; $ent_A->configure(-validatecommand => sub { first($ent_A, $ent_B) +}); $ent_B->configure(-validatecommand => sub { second($ent_A, $ent_B) + }); }

Again, you're free to take it or leave it, but I find the simplification makes a Tk program a lot easier to read. Plus, you can easily see where to change the 2 occurrences of "disabled" to "readonly", to see the resulting effect.

Good luck!

Update:   Removed some parameters left over from debugging the root cause of the problem.

Update 2:   Link directly to description of State in the Tk docs for the Entry widget.

say  substr+lc crypt(qw $i3 SI$),4,5

In reply to Re: Callback is being executed twice by Tk::entry widget by golux
in thread Callback is being executed twice by Tk::entry widget by perlingRod

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-20 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found