note
kevbot
Hello [Chuma],
<p>I was able to find a solution with some help from [id://987386], and [http://shop.oreilly.com/product/9781565927162.do|Mastering Perl Tk] <I>Chapter 9.6 - Creating Items in a Canvas - The Widget Item</I>.</p>
<code>
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/say/;
use Tk;
#create a couple of arbitrary widgets
my $main = MainWindow->new;
my $can = $main->Canvas(-width => 600, -height => 400)->pack;
my $lab = $can->Label(-text => "AAAAAAAA");
my $but = $can->Button(-text => "BBBBBBBB");
# The Label and Button methods used above do not have a '-tags' option. So as
# suggested by Mastering Perl/Tk, the createWindow method can be used to add
# tags
$can->createWindow(100, 100, -window => $lab, -tags => ['MyLabel']);
$can->createWindow(300, 150, -window => $but, -tags => ['MyButton']);
# use a key binding to invoke an event
$main->bind('<KeyPress-a>',sub{
# The method used below was shown in:
# http://www.perlmonks.org/?node_id=987386
$can->addtag( qw/current closest/, $Tk::event->x, $Tk::event->y );
my @tags = grep {$_ ne 'current'} $can->gettags(qw/current/);
$can->dtag(qw/current current/);
say @tags;
});
MainLoop;
</code>
1176576
1176576