473,394 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
Jul 18 '05 #1
5 3162
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' )
Jul 18 '05 #2
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.

"Gordon Airport" <uc*@ftc.gov> wrote in message
news:ek********************@comcast.com...
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' )

Jul 18 '05 #3

"Marc" <lo********@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='ye llow')
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()

Jul 18 '05 #4
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.
Jul 18 '05 #5
Type "point in polygon algorithm" into Google and follow the links.

Tim

Marc wrote:
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.


Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: lawrence | last post by:
2 Questions: 1.) Can anyone think of a way to speed up this function? It is terribly slow. I plan to reduce the number of directories to 3, which I guess will speed it up in the end. 2.) This...
15
by: Peter | last post by:
I really can't understand why you still use it or why you started using it in the first place.
11
by: Kamus of Kadizhar | last post by:
I have the following function which generates MD5 hashes for files on a local and remote server. The remote server has a little applet that runs from inetd and generates an MD5 hash given the file...
4
by: Ken Fine | last post by:
I'm looking to find or create an ASP script that will take a string, examine it for a search term, and if it finds the search term in the string, return the highlighted search term along with the...
11
by: Barney Norris | last post by:
Hi, The W3C validator tells me this page isn't valid HTML 4.01 Strict: http://www-student.cs.york.ac.uk/~jban100/wont_validate.html The reason it gives is I've closed meta tags with a '/'...
0
by: Lucky Carl | last post by:
DFS wrote: > You wiped XP but you're still obsessed with Windows. You need to wipe > your brain. Why does Microsoft pay this person to astroturf in COLA ?
383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
8
by: dick | last post by:
I am just trying to print/report the results of a "filter by selection" which is done by right-clicking a form, filling in values, and "applying the filter." I have searched the newsgroups, and...
10
by: Steven Matthew Bennett | last post by:
I don't know about the rest of the posters, but I came to this NG to learn more about Access, not to have some idiot homophobic dipshit spewing hate with every post. You have nothing to add to...
10
by: sklett | last post by:
I have a situation where I'm getting in Image that has a gray (solid, same color) background with a smaller white rectangle inside. The position is not always the same. What I need to do is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.