import tkinter as tk import pyautogui import threading import time # 创建主窗口 root = tk.Tk() root.title("鼠标坐标显示器") root.attributes("-topmost", True) # 窗口始终置顶 root.geometry("200x80") # 设置窗口大小 root.resizable(False, False) # 标签显示坐标 label = tk.Label(root, text="X: 0 Y: 0", font=("Arial", 16)) label.pack(expand=True) # 更新坐标的函数 def update_position(): while True: x, y = pyautogui.position() label.config(text=f"X: {x} Y: {y}") time.sleep(0.05) # 每50毫秒刷新一次 # 启动更新线程,防止界面卡顿 thread = threading.Thread(target=update_position, daemon=True) thread.start() # 启动Tkinter主循环 root.mainloop()