在 Pynput 中检索用于单击和释放的 x 和 y 鼠标位置
Retrieving x and y mouse positions for click and release in Pynput
我正在使用带有 pynput 的 Python 3.9.7,我想检索鼠标的 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
可能有更好的方法,但这行得通:
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
在上面的第一个事件中,我在按下后移动了鼠标。在第二个事件中,我只是点击了。
修改了好久的代码,终于成功了。
感谢 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
我正在使用带有 pynput 的 Python 3.9.7,我想检索鼠标的 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
可能有更好的方法,但这行得通:
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
在上面的第一个事件中,我在按下后移动了鼠标。在第二个事件中,我只是点击了。
修改了好久的代码,终于成功了。 感谢 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