From: Hidetoshi NAGAI Date: 2008-01-10T00:13:20+09:00 Subject: Re: Ruby/Tk - binding to canvas items From: Witold Rugowski Subject: Ruby/Tk - binding to canvas items Date: Wed, 9 Jan 2008 06:38:33 +0900 Message-ID: <31383eb79838802c2314f99d24282680@ruby-forum.com> > I have bunch of lines in one canvas. Event passed to code does refers > only to widget which was clicked, but this is canvas itself, not > particular canvas item. Is possible to know which line was clicked? On Tcl/Tk's canvas manual, -------------------------------------------------------------------- The tag current is managed automatically by Tk; it applies to the current item, which is the topmost item whose drawn area covers the position of the mouse cursor. If the mouse is not in the canvas widget or is not over an item, then no item has the current tag. -------------------------------------------------------------------- So, you should use 'current' to denote the target item. Or use a TkcTagCurrent object. For example, -------------------------------------------------------------------- require 'tk' c = TkCanvas.new.pack current = TkcTagCurrent.new(c) callback = proc{|w| p c p w p w.itemconfiginfo('current') p current.configinfo p w.find_withtag('current') } [100, 120, 140, 160, 180, 200].each{|y| TkcLine.new(c, [50, y], [200, y]).bind('1', callback, '%W') # or .bind('1', '%W', &callback) } Tk.mainloop -------------------------------------------------------------------- -- Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)