用python显示鼠标位置

发布于 2024-12-14 09:46:37 字数 514 浏览 1 评论 0原文

我想跟踪我的鼠标位置并在一个小窗口中显示它。 为此,我创建了这段代码:

#! /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2024-12-21 09:46:37
  1. 使用root.after定期调用update
  2. 使用 strl = tk.StringVar() 和 tk.Label(...,textvariable=strl) 来
    允许更改标签文本。
  3. 调用 strl.set() 更改标签文本。
  4. 添加了等于 display.Display().screen().rootscreenroot 默认值
    mousepos,这样大部分长链函数调用都是
    每次调用 mousepos 时不会重复。不带任何参数调用 mousepos() 将继续照常工作。

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()
  1. Use root.after to call update periodically.
  2. Use strl = tk.StringVar() and tk.Label(...,textvariable=strl) to
    allow the Label text to change.
  3. Call strl.set() to change the Label text.
  4. A default value for screenroot equal to display.Display().screen().root was added
    to mousepos so that most of that long chain of function calls are
    not repeated every time mousepos is called. Calling mousepos() without any arguments will continue to work as usual.

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文