在Pynput中获取鼠标点击和释放时的x和y坐标位置。

4

我正在使用Python 3.9.7和pynput,想要分别检索鼠标点击和释放时的x和y位置,并将其保存到函数on_click之外的变量中(例如:px =按下时的x位置,rx =释放时的x位置),以供其他函数使用。

以下是代码,代码已经根据Pynput文档进行了修改:

from pynput import mouse
import time

px = 0
py = 0
rx = 0
ry = 0

pressed_location = 0, 0
released_location = 0, 0

def on_click(x, y, button, pressed):
    global pressed_location
    global released_location
    global px,py,rx,ry
    
    if pressed:
        pressed_location = x, y
        px = x
        py = y
    else:
        released_location = x, y
        rx = x
        ry = y
        #debugging inside functions
        #print('You pressed at {0}x{1} and released at {2}x{3}'.format(*pressed_location, *released_location))
        print('Inside function pressed at {0}x{1}'.format(*pressed_location, *released_location))
        print('Inside function released at {2}x{3}'.format(*pressed_location, *released_location))
        return False
    
listener = mouse.Listener(on_click=on_click)
listener.start()

#debugging outside functions
print ("Outside function pressed: ", px , "x" ,py)
print ("Outside function released: ", rx , "x" , ry)  

然而,我被输出的结果难住了(在函数外面,它仍然显示为0,而在函数内部的变量却显示出实际的值)。示例结果如下: 我只想让函数内部的变量的值“传递”到函数外部的变量中。提前感谢您的帮助。
Outside function pressed:  0 x 0
Outside function released:  0 x 0
>>> Inside function pressed at 293x249
Inside function released at 768x815

rr


在你的 def 之前定义一些全局变量,然后使用 if else 语句代替 format,所以如果 pressed,则可以将你的 mx 值设置为它,否则将其设置为你的 mrx 值。 - iѕєρєня
mx = 0 mr = 0在“def”中使用: 如果被按下: mx = x 否则: mrx = x - iѕєρєня
2个回答

1

可能有更好的方法,但这个也行:

from pynput import mouse
import time

pressed_location = 0, 0
released_location = 0, 0

def on_click(x, y, button, pressed):
    global pressed_location
    global released_location
    if pressed:
        pressed_location = x, y
    else:
        released_location = x, y
        print('You pressed at {0}x{1} and released at {2}x{3}'.format(*pressed_location, *released_location))

listener = mouse.Listener(on_click=on_click)
listener.start()

while True:
    time.sleep(1)

我已创建一个全局变量pressed_location,并给它一个默认值。如果用户按下鼠标,位置将存储在其中。如果用户释放鼠标,我们会显示按下的位置和释放的位置。
示例输出:
You pressed at 103x412 and released at 299x559
You pressed at 207x478 and released at 207x478

在上面的第一个事件中,我按下后移动了鼠标。在第二个事件中,我只是点击了一下。

0

经过一段时间的代码调试,它终于可以工作了。 感谢 iѕєρєня 和 Robson 的帮助。

from pynput import mouse
import time

global pressed_location
global released_location
#Initialize
px = py = rx = ry = 0

def on_click(x,y, button, pressed):
    global px
    global py
    global rx
    global ry
    
    if pressed:
        px, py = x, y

    else:
        rx , ry = x, y

    if not pressed:
        print("Inside function pressed x : " ,px , " ", py)
        print("Inside function released y : " ,rx, " ", ry)

        return False
        return px
        return py
        return rx
        return ry

listener = mouse.Listener(
    on_click=on_click)
listener.start()

# some times needed for function to finish register the mouse location
time.sleep(3)
print("Outside function pressed x : " ,px , " ", py)
print("Outside function released y : " ,rx, " ", ry)

输出如下:

Inside function pressed x :  505   206
Inside function released y :  1084   892
Outside function pressed x :  505   206
Outside function released y :  1084   892

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接