Doing things anonymously just brings alot of confusion, but it can be done. You can always get the widget's internal identifier with $widget->id;
#! /usr/bin/perl -w
use warnings;
use strict;
use Tk;
my $mw=MainWindow->new();
foreach(1..10)
{
my $e = $mw->Entry()->pack;
$e->bind('<FocusIn>', sub {
print "@_\n";
print "my entry is $_[0]\n";
$_[0]->configure(-bg => 'hotpink');
} );
}
MainLoop;
but you are far better off storing everthing in hashes, because you can then get access to each entry from anywhere in the program.
#! /usr/bin/perl -w
use warnings;
use strict;
use Tk;
my $mw=MainWindow->new();
my %entries;
foreach(1..10)
{
$entries{$_}{'widget'} = $mw->Entry()->pack;
$entries{$_}{'widget'}->bind('<FocusIn>', sub {
$_[0]->configure(-bg => 'hotpink');
});
}
$mw->after(5000,sub{
foreach(2,4,6,8){
$entries{$_}{'widget'}->configure(-bg => 'hotpink');
}
});
MainLoop;
|