Python Forum
[Tkinter] Finding out what's under the cursor - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Finding out what's under the cursor (/thread-25330.html)



Finding out what's under the cursor - sabresong - Mar-27-2020

I'm new to Python, and way out of practice (15+ years out of practice) with any sort of programming. I don't have example code as I'm in the planning phase, and I'm not really sure how to ask what I'm looking for, so please bear with me.

I'm working on a project that will have a canvas. The user will be able to draw boxes and assign properties to the boxes.

I'd like to know if there's a way to detect whether a user's box exists at the mouse coordinates so that I can take one set of actions if there's no box there and get the user-defined properties of the box if there is one.

Most of this I can figure out. But how does one detect whether there's a box under the cursor if the box isn't a tkinter widget?


RE: Finding out what's under the cursor - deanhystad - Mar-27-2020

Why wouldn't it be a tkinter (or some other GUI tookit) widget? The base class for widgets is a thing that knows what to do when you click on it or type in it. Sounds like that is something you want. If you need a different appearance you can subclass an existing widget and override the paint method.


RE: Finding out what's under the cursor - Larz60+ - Mar-27-2020

use:
x = root.winfo_pointerx() - root.winfo_rootx()
y = root.winfo_pointery() - root.winfo_rooty()



RE: Finding out what's under the cursor - sabresong - Mar-28-2020

Thank you! I think I was a little confused about widgets.