screen_percentage.py 915 B

123456789101112131415161718192021222324252627282930313233
  1. # -*- coding: utf-8 -*-
  2. import pyautogui
  3. import tkinter as tk
  4. def update_position():
  5. x, y = pyautogui.position()
  6. screen_width, screen_height = pyautogui.size()
  7. x_percent = (x / screen_width) * 100
  8. y_percent = (y / screen_height) * 100
  9. label.config(text=f"X: {x_percent:.2f}%\nY: {y_percent:.2f}%")
  10. root.after(100, update_position) # 每100毫秒更新一次
  11. # 创建主窗口
  12. root = tk.Tk()
  13. root.title("鼠标位置")
  14. # 设置窗口大小(稍微大一点)
  15. root.geometry("160x80") # 宽度200像素,高度100像素
  16. # 设置字体大小(稍微大一点)
  17. font_style = ("Arial", 14) # 使用Arial字体,字号12
  18. # 创建一个标签用于显示鼠标位置百分比
  19. label = tk.Label(root, text="等待中...", font=font_style)
  20. label.pack(pady=10) # 垂直方向上留出10像素的间距
  21. # 启动更新位置的函数
  22. update_position()
  23. # 运行主循环
  24. root.mainloop()