merrymonk has asked for the wisdom of the Perl Monks concerning the following question:

I want to make check the contents of an Entry box after the user has finished entering data. The tab key moves the focus from the current Entry box but sometimes it is not sensible to use the tab key. Therefore the mouse can be used. If I use
$widget->focusFollowsMouse;
the focus model of application is changed so that the focus follows the mouse pointer. This is great since it gets the focus out of the current widget when the mouse pointer moves out of the widget. However, if the mouse goes across another Entry box which can be checked, a check is done whether or not it should happen.
Is there a way I can reset the focus model to ‘undo’ the focusFollwosMouse statement?

Replies are listed 'Best First'.
Re: Tk - Focus & Mouse - On & Off
by thundergnat (Deacon) on Mar 09, 2015 at 17:17 UTC

    Rather than trying to enable / disable focusFollowsMouse, you may be better off putting in a small "autofocus" procedure and only bind it to the widgets you want. Something like this: (probably buggy but should give the the idea)

    #!/usr/bin/perl use strict; use warnings; use Tk; my $mw = MainWindow->new; my $entry = $mw->Entry()->pack; my $entry1 = $mw->Entry()->pack; my $entry2 = $mw->Entry()->pack; autofocus($_) for ($entry, $entry1, $entry2); sub autofocus { my $widget = shift; $widget->bind( '<Enter>' => sub{ $widget->focus } ); } MainLoop;
      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.

        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 "