#!/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('',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;