您现在的位置是:网站首页 > 博客日记 >

python获取屏幕坐标值

作者:YXN-python 阅读量:3 发布日期:2024-06-20

from pynput.mouse import Listener


def selection_area():
    # 存储最近的两次点击坐标
    clicks = []
    click_count = 0  # 点击计数器

    def on_click(x, y, button, pressed):
        nonlocal click_count, clicks
        if pressed:
            # 记录点击坐标
            clicks.append((x, y))
            click_count += 1
            print(f"点击坐标为:{x},{y}")
            # 如果已经获取了两次点击,停止监听
            if click_count >= 2:
                return False

    def on_scroll(x, y, dx, dy):
        pass

    print("请点击两次以选择截图区域...")
    # 监听鼠标事件
    with Listener(on_click=on_click, on_scroll=on_scroll) as listener:
        listener.join()

    return clicks


if __name__ == "__main__":
    selection_area()

YXN-python

2024-06-20