[Mpls-pm] Issue with binding events to Tk canvas objects.

Jay O'NEIL JO'Neill at pgicompanies.com
Wed Sep 22 13:58:13 CDT 2004


OS = WinXP

Dear Experts,

I was wondering if someone could help me with this Tk issue. I need to bind events
to text objects on a canvas. There are potentially hundreds of these text objects,
and would like to do the bind as each one is created in a loop. The problem seems
to come when I use a variable for the index number. The top lines of code work as
I would expect them to -- when the cursor enters the text object it turns red. If
I try to use the second bunch of lines I get a strange result -- all of the objects
I try to bind act on the last object only, i.e. when the cursor moves into the object
with index 2 or 3 or 4, the object with index 4 turns red. I'm baffled, and I'm 
probably doing something that is completely wrong. I don't use binding that often,
so I might be missing the whole point.

# this works as expected
$canvas->bind(2,"<Enter>",sub{&stuff(2);});
$canvas->bind(3,"<Enter>",sub{&stuff(3);});
$canvas->bind(4,"<Enter>",sub{&stuff(4);});

# this does not work as expected
$foo = 2;
$canvas->bind($foo,"<Enter>",sub{&stuff($foo);});
$foo = 3;
$canvas->bind($foo,"<Enter>",sub{&stuff($foo);});
$foo = 4;
$canvas->bind($foo,"<Enter>",sub{&stuff($foo);});
# this is the only object that gets acted upon

sub stuff
{
    my $index = shift;
    $canvas->itemconfigure($index,-fill=>'red');
}

If someone could point me in the right direction I would really appreciate it.

Jay O'Neill
jayzenx at earthlink.net





More information about the Mpls-pm mailing list