python实现鼠标实时坐标监测

python实现鼠标实时坐标监测

一、说明

使用了以下技术和库:

  1. tkinter:用于创建GUI界面。
  2. pyperclip:用于复制文本到剪贴板。
  3. pynput.mouse:用于监听鼠标事件,包括移动和点击。
  4. threading:用于创建多线程,以便在后台执行鼠标事件监听和标签更新的任务。
  5. time:用于控制线程休眠,以定时更新标签文本。

二、代码

# coding=gbk  # 指定文件编码为GBK
import tkinter as tk  # 导入tkinter库,用于创建GUI界面
import pyperclip  # 导入pyperclip库,用于复制文本到剪贴板
from pynput import mouse  # 导入pynput库的mouse模块,用于监听鼠标事件
import threading  # 导入threading库,用于创建多线程
import time  # 导入time库,用于线程休眠

# 创建一个MouseCoordinateApp类,用于处理鼠标坐标显示和复制
class MouseCoordinateApp:
    def __init__(self):
        self.root = tk.Tk()  # 创建一个Tkinter窗口
        self.root.title("鼠标坐标实时展示")  # 设置窗口标题
        self.root.geometry("350x80")  # 设置窗口大小
        self.root.resizable(False, False)  # 禁止窗口大小调整
        self.label = tk.Label(self.root, text="单机截取坐标:X: - , Y: -\n实时坐标:X: - , Y: -")  # 创建一个标签控件
        self.label.pack()  # 将标签控件添加到窗口
        copy_button = tk.Button(self.root, text="复制坐标", command=self.copy_coordinates)  # 创建一个按钮控件
        copy_button.pack()  # 将按钮控件添加到窗口
        self.root.attributes("-topmost", True)  # 设置窗口置顶
        self.extracted_coordinates = (0, 0)  # 初始化截取坐标
        self.current_coordinates = (0, 0)  # 初始化实时坐标
        self.last_extracted_coordinates = (0, 0)  # 初始化上一次截取的坐标
        self.update_interval = 0.1  # 更新标签的时间间隔
        threading.Thread(target=self.start_mouse_listener, daemon=True).start()  # 创建并启动鼠标事件监听的线程
        threading.Thread(target=self.update_label_thread, daemon=True).start()  # 创建并启动标签更新的线程

    def copy_coordinates(self):
        x, y = self.last_extracted_coordinates  # 获取上一次截取的坐标
        coordinates = f"X: {x}, Y: {y}"  # 格式化坐标文本
        pyperclip.copy(coordinates)  # 复制坐标文本到剪贴板
        self.label.config(text=f"已复制坐标:{coordinates}")  # 更新标签文本

    def start_mouse_listener(self):
        with mouse.Listener(on_move=self.on_move, on_click=self.on_click) as listener:
            listener.join()  # 启动鼠标事件监听

    def on_move(self, x, y):
        self.current_coordinates = (x, y)  # 更新实时坐标

    def on_click(self, x, y, button, pressed):
        if pressed:
            self.last_extracted_coordinates = self.extracted_coordinates  # 更新上一次截取的坐标
            self.extracted_coordinates = (x, y)  # 如果鼠标被按下,更新截取坐标

    def update_label_thread(self):
        while True:
            time.sleep(self.update_interval)  # 线程休眠一段时间
            self.update_label()  # 更新标签文本

    def update_label(self):
        extracted_x, extracted_y = self.extracted_coordinates  # 获取截取坐标
        current_x, current_y = self.current_coordinates  # 获取实时坐标
        self.label.config(text=f"截取坐标:X: {extracted_x}, Y: {extracted_y}\n实时坐标:X: {current_x}, Y: {current_y}")

    def run(self):
        self.root.mainloop()  # 启动主程序的主循环

if __name__ == "__main__":
    app = MouseCoordinateApp()  # 创建MouseCoordinateApp实例
    app.run()  # 启动应用程序的主循环

三、效果

在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值