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.


In reply to Identifying Tk::Canvas Items Bound to Events by Dr. Mu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.