Dr. Mu has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a Tk app in which the entire window is a canvas representing a control panel. On this canvas are three images representing toggle switches, each of which may display a GIF of a switch in either the up or down position. For efficiency's sake, I would like to make one binding to all these switches, via their common tag 'SWITCH', to swap images when the switch receives a button event. What I can't seem to find, though, is how to identify which switch image received the event so I know which one to toggle. The first argument to the event subroutine is a reference to the entire canvas, not to the item within the canvas that received the event.* Is there a property of the canvas object that I'm unaware of that will identify which item received the event?

*This behavior is at odds, by the way, with the description in O'Reilly's Mastering Perl/Tk. There, $_[0] is stated to be the first explicit argument in the structure, '<Button>' => [sub {...}, $arg0, $arg1]. In fact, $arg0 will come through as $_[1]. $_[0] is always a reference to the widget receiving the event.

  • Comment on Identifying Tk::Canvas Items Bound to Events

Replies are listed 'Best First'.
Re: Identifying Tk::Canvas Items Bound to Events
by eserte (Deacon) on Apr 19, 2004 at 08:39 UTC
    Use the "current" pseudo tag (e.g. $canvas->find(withtag => "current"))
      Gotta love those short answers! Yes, it worked beautifully. Thanks!