用python显示鼠标位置
我想跟踪我的鼠标位置并在一个小窗口中显示它。 为此,我创建了这段代码:
#! /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()
这个小脚本在启动时显示鼠标位置,但在鼠标移动时不会刷新它。我不明白(我有说过我是Python新手吗?)。 我想我必须使用 Xlib 中的一个事件来告诉我的脚本鼠标何时移动...
如何刷新我的鼠标位置?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
root.after
定期调用update
。允许更改标签文本。
strl.set()
更改标签文本。display.Display().screen().root
的screenroot
默认值到
mousepos
,这样大部分长链函数调用都是每次调用 mousepos 时不会重复。不带任何参数调用
mousepos()
将继续照常工作。root.after
to callupdate
periodically.strl = tk.StringVar()
andtk.Label(...,textvariable=strl)
toallow the Label text to change.
strl.set()
to change the Label text.screenroot
equal todisplay.Display().screen().root
was addedto
mousepos
so that most of that long chain of function calls arenot repeated every time
mousepos
is called. Callingmousepos()
without any arguments will continue to work as usual.