I am using Python 3.9.7 with pynput, I wanted to retrieve the x and y position of mouse both clicking and releasing separately and saved it into variables outside of the function on_click (e.g: px = x position when pressed, rx = x position when released) for other function usage
The code are as follows , code are modified from 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)
However I am stumped as with the output (outside the functions as it still shows 0 whereas as the variable inside the function is showing actual values.)Sample results is as followed: I just wanted the values of variable inside the function can be "transferred" to the varible outside of the functions. Thanks in advance.
Outside function pressed: 0 x 0
Outside function released: 0 x 0
>>> Inside function pressed at 293x249
Inside function released at 768x815
rr