3

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.

Aleksandr Kovalev
  • 3,508
  • 4
  • 34
  • 36
Well3
  • 121
  • 5

3 Answers3

7

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.
Deacon
  • 3,615
  • 2
  • 31
  • 52
Well3
  • 121
  • 5
3

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 .

coder3521
  • 2,608
  • 1
  • 28
  • 50
0

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.

Fran Lupión
  • 134
  • 10