From: Joel VanderWerf Date: 2008-01-09T11:21:25+09:00 Subject: Re: Ruby/Tk - binding to canvas items Witold Rugowski wrote: > Hi! > I have canvas items (TkcLine to be exact) and I need to bind some code > executed on click. I use code similar to this: > > line = TkcLine.new($root, arg, :fill => '#000', :width => 2) > line.bind("Button-1") {|e| > #some actions > } > > It works, but I dont know how to get which item was clicked. I just tested that this works: line.bind("Button-1") {|e| p line } It's a closure, so you can refer to local scope. There's one thing to watch out for: line = TkcLine.new... line.bind("Button-1") {|e| p line } line = TkcLine.new... line.bind("Button-1") {|e| p line } This creates two lines, but the scopes share the same binding for the local var "line", so the two blocks will both operate on the second line. Instead, use different var names, or use an #each loop. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407