Is it possible to track a mouse pointer full screen in Python?

I want to keep track of the whole screen, not limited to just my GUI.

I used to do this in C and MATLAB, but now I work in Python and Tkinter.

+3


source to share


3 answers


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



import Tkinter as tk
root = tk.Tk()

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

      

+6


source


Try the below example code, it will help you understand better

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.

+3


source


I believe you need to use some system libraries to do this, since Tkinter probably only keeps track of where the pointer is inside the main window.

There is a library for this purpose and a lot more functionality called PyUserInput

Hope this is what you were looking for.

0


source







All Articles