问题描述
我想知道是否有一种方法可以继续在tkinter窗口上显示实时鼠标的位置。我知道如何找到鼠标坐标。
x,y = win32api.GetCursorPos()
mousecords = Label(self.root,text='x : ' + str(x) + ',y : ' + str(y))
mousecords.place(x=0,y=0)
但是我需要标签在鼠标移动时保持更新。帮助将不胜感激 谢谢!
解决方法
这将仅在鼠标位于tkinter窗口内时更新let tableEl = document.getElementById("tableId");
let index = 0;
let colors = ["#e7eff6","#d0e1f9","#adcbe3"]
for (let trEl of tableEl.rows) {
trEl.style.backgroundColor = colors[index % colors.length];
index++;
}
:
不需要使用win32api,tkinter内置了它。我们可以将函数绑定到Label
的{{1}}键上,并使用给定的位置参数root
来检索鼠标。
<Motion>
,
您可以使用after()
定期获取鼠标坐标并更新标签。
下面是一个示例:
import tkinter as tk
import win32api
root = tk.Tk()
mousecords = tk.Label(root)
mousecords.place(x=0,y=0)
def show_mouse_pos():
x,y = win32api.GetCursorPos()
#x,y = mousecords.winfo_pointerxy() # you can also use tkinter to get the mouse coords
mousecords.config(text=f'x : {x},y : {y}')
mousecords.after(50,show_mouse_pos) # call again 50ms later
show_mouse_pos() # start the update task
root.mainloop()