2 回答

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊
這只會(huì)在Label鼠標(biāo)位于 tkinter 窗口內(nèi)時(shí)更新:
無(wú)需使用 win32api,tkinter 內(nèi)置了它。我們可以將一個(gè)函數(shù)綁定到root的<Motion>鍵上,并使用給定的位置參數(shù)event來(lái)檢索鼠標(biāo)的坐標(biāo)。
from tkinter import Tk, Label
root = Tk()
label = Label(root)
label.pack()
root.bind("<Motion>", lambda event: label.configure(text=f"{event.x}, {event.y}"))
root.mainloop()

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊
您可以使用它after()來(lái)定期獲取鼠標(biāo)坐標(biāo)并更新標(biāo)簽。
下面是一個(gè)例子:
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()
添加回答
舉報(bào)