I want to track my mouse-position and show that in a tiny window.
For that, I created this piece of code:
#! /usr/bin/python
from Tkinter import *
from Xlib import display
def mousepos():
data = display.Display().screen().root.query_pointer()._data
return data["root_x"], data["root_y"]
root = Tk()
strl = "mouse at {0}".format(mousepos())
lab = Label(root,text=strl)
lab.pack()
root.title("Mouseposition")
root.mainloop()
This little script shows the mouse-position on startup but doesn’t refresh it on mouse-movement. I don’t get behind it (did I say that I’m new to python?).
I think I have to use an event from Xlib that tells my script when the mouse is moving…
How do I refresh my mouse-position?
root.afterto callupdateperiodically.strl = tk.StringVar()andtk.Label(...,textvariable=strl)toallow the Label text to change.
strl.set()to change the Label text.screenrootequal todisplay.Display().screen().rootwas addedto
mouseposso that most of that long chain of function calls arenot repeated every time
mouseposis called. Callingmousepos()without any arguments will continue to work as usual.