in reply to Re: Tk - Focus & Mouse - On & Off
in thread Tk - Focus & Mouse - On & Off

That works fine for getting the focus in the widget when the cursor enters the widget. The problem is knowing when the cursor has left the widget if it does not go into another widget which has this bind. The problem is made more difficult since there are a number widgets with this bind but only a certain number should be recognised at a specific time. Then later on another set of widgets should be recognised. This was why I was trying to find a way to use the focusFollowsMouse which would let me detect the cursor had left a widget and then disable this function until an Entry widget had been 'entered'.
I know there also is a Leave function which detects when the mouse has left a widget. An alternate would be to use this but then I do not know how to take away the Focus from a widget without another widget having the focus.

Replies are listed 'Best First'.
Re^3: Tk - Focus & Mouse - On & Off
by Anonymous Monk on Mar 09, 2015 at 21:58 UTC

    but then I do not know how to take away the Focus from a widget without another widget having the focus.

    a window is a widget

    perl -MTk -e " $mw = tkinit; $b = $mw->Button(-command=>sub{$mw->focus +})->pack; $b->focus; MainLoop "
      Many thanks for that. It is so obvious when you see it! ..but not before!!! The code below uses this latest input with the Perl suggested by an annonymous monk on 24th February to do what I want.
      The user has to either use the mouse or tab to get into an Entry box, enter a number and then use either the mouse or tab to leave the Entry box. At this point the value in the Entry box is checked against defined min and max. Messages are then printed to the MSDOS screen to give the state of the value.
      #!/usr/bin/perl -w use strict; use Tk; my (@entries, @entry_value); # max and min values for Entry boxes my (%maxev, %minev); $maxev{0} = 100; $maxev{1} = 150; $maxev{2} = 200; $minev{0} = 10; $minev{1} = 15; $minev{2} = 20; my $mw = MainWindow->new; my $frame = $mw->Frame()->pack(-padx => 50,-pady => 50); foreach my $i (0..2) { $entry_value[$i] = "$i"; $entries[$i] = $frame->Entry(-relief => 'raised', -textvariable => +\$entry_value[$i], )->pack(-side => 'left', -padx => 50,-pady + => 50); # $entries[$i]->bind('<Enter>' => [\&sayHello,$i]); $entries[$i]->bind('<Leave>' => [\&sayGoodbye,$i]); $entries[$i]->bind('<FocusIn>' => [\&sayFocusIn,$i]); + $entries[$i]->bind('<FocusOut>' => [\&sayFocusOut,$i]); } MainLoop; sub sayHello { my $w = shift; my $id = shift; $w->focus; $frame->grab; print "Hi, I am entry $id!\n"; } sub sayGoodbye { my $w = shift; my $id = shift; $frame->grab; $mw->focus; print "Goodbye, I was entry $id!\n"; } sub sayFocusIn { my $w = shift; my $id = shift; $frame->grab; print "Hi, FocusIn said I am entry $id - value $entry_value[$id]\n" +; } sub sayFocusOut { my $w = shift; my $id = shift; my $val_ok; $frame->grab; $val_ok = 'yes'; # check that current valaue is within the min max values if($entry_value[$id] < $minev{$id}) { print "\nEntry $id - value is less than allowed minimum of $minev{ +$id}\n"; $val_ok = 'no'; } if($entry_value[$id] > $maxev{$id}) { print "\nEntry $id - value is greater than allowed maxmum of $maxe +v{$id}\n"; $val_ok = 'no'; } if($val_ok eq 'yes') { print "\nEntry $id - value is acceptable\n"; } }

        Ok, now I see better what you are trying to accomplish... Seems to me like it would be easier to just use the validateCommand and invalidCommand options in the Entries.

        See below: Of course the parameters of what constitutes a "bad" entry probably need to be adjusted.

        Minor edit: regularized some variables.

        #!/usr/bin/perl -w use strict; use Tk; use Tk::Dialog; my %w; # widget hash my @entries = ( { max => 100, min => 10, value => 50 }, { max => 150, min => 15, value => 75 }, { max => 200, min => 20, value => 110 }, ); $w{mw} = MainWindow->new; $w{frame} = $w{mw}->Frame->pack( -padx => 50, -pady => 50 ); for my $i ( 0 .. 2 ) { ${w}{entries}[$i]{name} = $w{frame}->Entry( -relief => 'raised', -textvariable => \$entries[$i]{value}, -validate => 'focusout', -vcmd => sub { $_[0] !~ /\D/ && $_[0] <= $entries[$i]{max} && $_[0] >= $entries[$i]{min}; }, -invcmd => sub { $w{invalid}->configure( -text => ( $_[0] =~ /\D/ ) ? 'Not an integ +er' : ( $_[0] > $entries[$i]{max} ) ? 'Number too l +arge' : ( $_[0] < $entries[$i]{min} ) ? 'Number too s +mall' : 'Bogus entry' ); $w{invalid}->Show; ${w}{entries}[$i]{name}->focus; }, )->pack( -side => 'left', -padx => 50, -pady => 50 ); } $w{invalid} = $w{mw}->Dialog( -title => 'Invalid entry' ); MainLoop;
        Here is closure free version  perl -MTk -e " tkinit->Button(-command=>sub{ $Tk::widget->parent->focus })->pack ->focus; MainLoop "