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

I have a GUI built using Perl Tk and laid out using the grid geometry manager. Therefore I do not define the positions of the widgets using co-ordinates on the screen. However, chapter 13 of Mastering Perl Tk shows that various types of position of widgets can be obtained. I have 2 problems associated with cursor position. These are:
1. Knowing when the cursor has moved out of an entry box;
2. Wanting to draw a symbol when the cursor is over a disabled widget such as a radio button.

Knowing when the cursor has moved out of an entry box
I have a set of entry boxes which into which users enter a number. I want to test if the number is greater than a minimum value and less than a maximum value. I can test maximum criterion before the cursor leaves the entry box. I have to leave the entry box before I can test the minimum criterion. I can use the tab key to go from box to box. This works for all but the last box when I use the mouse to move the cursor out of the entry box. I have implemented a bind which is based on Focus and this works when I use the tab as the focus is then in the next box. However, it does not work when I use the mouse because the focus does not change.
I have thought of monitoring the cursor position and calculating when the cursor is out of the rectangular area of the entry box.
Two questions
1. Will this work;
2. Is there a simpler method?

Drawing a symbol when the cursor is over a disabled widget such as a radio button
I have seen an application on the web where when the cursor is over a disabled widget, a small red circle with a diagonal line across it is shown to the user as a positive indication that the disabled widget cannot be used. I would like to implement a similar feature. In a similar way to the Focus out problem above I have thought of monitoring the cursor position and calculating when the cursor is ‘above’ the disabled widget and then showing a cursor symbol of the red circle and line.
Two questions
1. Will this work;
2. Is there a simpler method?

Any examples of answers to either problem or pointers to them will be more than gratefully received.

Replies are listed 'Best First'.
Re: Perl Tk - 2 cursor position problems
by Anonymous Monk on Feb 24, 2015 at 15:28 UTC
    I'm not sure what your calculations need to be, but would something like this give you any ideas?
    #!/usr/bin/perl -w use strict; use Tk; my @entries; my $mw = MainWindow->new; my $frame = $mw->Frame()->pack(-padx => 10,-pady => 10); foreach my $i (0..2) { $entries[$i] = $frame->Entry(-relief => 'raised', -text => "Entry $i")->pack(-side => 'left' +); $entries[$i]->bind('<Enter>' => [\&sayHello,$i]); $entries[$i]->bind('<Leave>' => [\&sayGoodbye,$i]); } MainLoop; sub sayHello { my $w = shift; my $id = shift; $frame->grab; print "Hi, I am entry $id!\n"; } sub sayGoodbye { my $w = shift; my $id = shift; $frame->grab; print "Goodbye, I am entry $id!\n"; }
      That seems a super start for the entry box problem as I have tried it and get 'good' messages when the cursor goes in and out of the entry boxes!
      I can see it probably can be used for the disabled entry box as well.
      I am thinking how I can use it for disabled widgets such as radio button etc.
      Many thanks