Well3
Well3

Reputation: 137

Can I track the mouse pointer over the entire screen in Python?

I wish to follow to mouse over the entire screen, not just limited to my GUI.

I used to be able to do it in C, and MATLAB, but now I'm working in Python and Tkinter.

Upvotes: 3

Views: 1083

Answers (3)

Well3
Well3

Reputation: 137

Silly me -- it's really easy, you don't even need a GUI running.

import Tkinter as tk
root = tk.Tk()

root.winfo_pointerx()  # this returns the absolute mouse x co-ordinate.

Upvotes: 7

coder3521
coder3521

Reputation: 2646

Try the below sample code it will help to get you a better understanding

import Tkinter as tk
import Xlib.display as display

def mousepos(screenroot=display.Display().screen().root):
    pointer = screenroot.query_pointer()
    data = pointer._data
    return data["root_x"], data["root_y"]

def update():
    strl.set("mouse at {0}".format(mousepos()))
    root.after(100, update)

root = tk.Tk()
strl = tk.StringVar()
lab = tk.Label(root,textvariable=strl)
lab.pack()
root.after(100, update)
root.title("Mouseposition")
root.mainloop()

Also please comment if you have any doubts .

Upvotes: 3

Fran Lupión
Fran Lupión

Reputation: 134

I believe you need to use some system libraries for that, as Tkinter probably only tracks where the pointer is inside the main window.

For that purpose and a lot more functionality there is a library called PyUserInput

I hope it is what you were looking for.

Upvotes: 0

Related Questions