【问题标题】:Retrieving x and y mouse positions for click and release in Pynput在 Pynput 中检索用于单击和释放的 x 和 y 鼠标位置
【发布时间】:2021-11-03 16:54:30
【问题描述】:

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

代码如下,代码由Pynput documentations修改:

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 语句代替格式化,因此如果 pressed 然后您可以设置 mx 值,否则将其设置为 mrx 值
  • 类似这样的:mx = 0 mr = 0 并在 def 中使用:if pressed: mx = x else: mrx = x

标签: python pynput


【解决方案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

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

【讨论】:

    【解决方案2】:

    在修改代码一段时间后,它可以工作了。 感谢 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      相关资源
      最近更新 更多