in reply to Tk and mouse position

Tk runs by MainLoop; it only talks to your app via events. The trick is to bind some event to a subroutine in your app, and then access the coordinates you want (probably via the x and y methods, or possibly rootx and rooty methods, of the Tk::event object your receive).

A simple example

#!/usr/local/bin/perl -w use strict; use Tk; my $mw = new Tk::MainWindow; my $c = $mw->Canvas->pack; my $tx = $c->createText(20, 10, -text => 'X: UNKNOWN'); my $ty = $c->createText(20, 25, -text => 'Y: UNKNOWN'); $c->Tk::bind('<Motion>' => [sub { my ($e,$x,$y) = (@_); $c->itemconfigure($tx, -text => "X: $x"); $c->itemconfigure($ty, -text => "Y: $y"); }, Ev('x'), Ev('y')]); MainLoop;

Replies are listed 'Best First'.
Re: Re: Tk and mouse position
by Popcorn Dave (Abbot) on May 02, 2002 at 19:36 UTC
    Thanks for that!

    But I do have a couple of questions:

    1. I understand everything you've done except what $e does. Are there some docs that you can point me at to explain that particular point?
    2. What is the advantage or disadvantage in using pointerxy as opposed to what you wrote?