单击鼠标时的坐标-Python

问题描述 投票:-1回答:1

我想创建一个Python脚本来存储鼠标单击的坐标。坐标应保存在文件中。我遇到了以下线程,但无法正常工作。该脚本应捕获整个桌面而不是特定窗口/应用程序的鼠标坐标。

Python get mouse x, y position on click

谢谢

python mouseevent coordinates
1个回答
0
投票

尝试此解决方案:

from pynput import mouse

def on_click(x, y, button, pressed):
     print(x,y)

with mouse.Listener(on_click=on_click) as listener:
     listener.join()

以上代码的结果是:

830 345 Button.left True
830 345 Button.left False

上面的代码的值将打印两次,即一次按下按钮的坐标值,一次按下按钮的坐标值。

因此要在单击按钮的位置打印坐标值,请使用以下代码:

from pynput import mouse

def on_click(x, y, button, pressed):
    if pressed == True:
        print(x,y)

with mouse.Listener(on_click=on_click) as listener:
     listener.join()
© www.soinside.com 2019 - 2024. All rights reserved.