Ruby/Tk - binding to canvas items

  • Thread starter Witold Rugowski
  • Start date
W

Witold Rugowski

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 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?

Best regards,
 
J

Joel VanderWerf

Witold said:
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.
 
W

Witold Rugowski

Joel said:
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.

Yeah, use of #each was required, since I was iterating through array and
needed index of elements inside of handler.

Thank You.
 
H

Hidetoshi NAGAI

From: Witold Rugowski <[email protected]>
Subject: Ruby/Tk - binding to canvas items
Date: Wed, 9 Jan 2008 06:38:33 +0900
Message-ID: said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top