mbilal25tr
mbilal25tr

Reputation: 141

Tkinter3 - Asking pointer position on the window?

I already know about 'winfo_pointerx()' and 'winfo_pointery()' but these functions give the position of pointer on the whole screen. I want to know if pointer is on the window or not. And I thought a function like that could help me. But maybe there is an easier one! Is there?

Upvotes: 1

Views: 111

Answers (1)

Andath
Andath

Reputation: 22724

I want to know if pointer is on the window or not

If I understand the goal you are trying to achieve, you can use the right events and bind them to your mouse as follows:

import tkinter as tk


class MousePointerInsideOrOutsideWindow:
    def __init__(self, master):       
        master.bind("<Enter>", lambda event: print("Mouse pointer INSIDE main window"))
        master.bind("<Leave>", lambda event: print("Mouse pointer OUTSIDE main window"))


if __name__ == '__main__':
   root=tk.Tk()
   app = MousePointerInsideOrOutsideWindow(root)
   root.mainloop()

You chose the Enter and Leave events to know if the mouse pointer, respectively, enters into or leaves the main window of your GUI.

Upvotes: 2

Related Questions