Canvas - Rectangle. Is there an easy way to detect if you're inside?

M

Marc

Hi all,

I've been using the canvas quite a bit lately, and I am wondering if I
am just missing a quick trick somewhere.

With most canvas objects you can bind then to mouse functions and
quickly determine if you are selecting them. However, the rectangle
(and I'm sure all other objects that aren't solid) only register if
you actually hit the frame of the object, if it's transparent. If you
fill it with color then it will register anywhere.

At the moment I am doing as series of if-then statements involving
coordinates and the positions that the rectangles hold to determine if
the mouse is somewhere inside the rectangle. However it would be much
simpler if there was a way to quickly recognize the internal area of a
rectangle using a canvas function.

Is there an easier way to do this? I've looked at all the canvas
functions and none really make this process easier unless the
rectangle can auto-recognize itself.

Many Thanks,
Marc
 
G

Gordon Airport

Have you tried find_closest?
Given a click event...

x = cv.canvasx( event.x )
y = cv.canvasy( event.y )
cvid = cv.find_closest( x, y )[0]
cv.itemconfig( cvid, fill = 'black' )
 
M

Marc

The problem is that I have other objects in the rectangle that I'm basically
using as a holder. So if I use the closest function, then sometimes when I
have more than one object in the rectangle it will pick up the other
objects. Therefore I simply need a way of always knowing when the cursor is
within the bounds of the rectangle.

Like I say, I am doing it right now with a bunch of if statements. It works
fine. But it's sort of a brute force method. Seems kind of hackish. It just
seems like one of those things where there has to be a cleaner way.
 
M

Michael Peuser

Like I say, I am doing it right now with a bunch of if statements. It works
fine. But it's sort of a brute force method. Seems kind of hackish. It just
seems like one of those things where there has to be a cleaner way.
Have you tried using 'tags'. These are widely underestimated.
There is the special tag 'current' which is bound to the object under the
cursor. The 'object' however is only the drawn area, i.e. you have to 'fill'
it.

Useful methods are gettags, find_withtag; even tag_bind might be useful!
There can be a list of tags to each object of course and they can be
manipulated in multiple ways (

Kindly
Michael P

Try this:

from Tkinter import *

c=Canvas(width=300,height=300)
c.create_rectangle(10,10,280,280,tags='a',fill='yellow')
c.create_rectangle(100,100,200,200,tags='b',fill='green')
c.pack()

def aMove(ev):
print c.gettags('current')

c. bind("<Motion>",aMove)
c.mainloop()
 
M

Marc

Yeah, I've had to use the 'tags' parameter fairly heavily to bind
different groups of canvas objects to different mouse actions. So I'm
all over that.

But I think the key thing you said was "has to be filled." Basically
I'm using the rectangle as a border within which other things are
placed. The border itself is simply an overlay where I need the
background to show through. Therefore I can't fill it with anything.

So if it has to be filled, apparently there is no easier way than the
way I'm doing now.
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top