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

Michael Peuser mpeuser at web.de
Fri Sep 12 03:45:52 EDT 2003


"Marc" <losnations at comcast.net>
.....

> 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()







More information about the Python-list mailing list