Return the X and Y values from the on_mouse function

42 Views Asked by At

I am trying to find a value using the location of the mouse cursors click, using pixel coordinates. The problem is that I have a function, def on_mouse(event, x, y).

There are other things here inside the function, but the crux of the problem is that I don’t have any way to retrieve the x and y values from inside of the function, which gives me x and y values depending on where I clicked.

So when outside of the function I do x + y, it says x and y are undefined.

I’ve tried everything I can think of, returning x, returning y, but I think I’m being dumb, to be honest. Im sure there’s a simple solution and I’m just being silly.

1

There are 1 best solutions below

0
Jonathan On

use the args to enter parameters for the event:

def on_mouse(event, x, y):
    global click
    global xd
    global yd
    click, xd, yd = True, x, y    
click = False
xd = 0
yd = 0
while True:
    if click == True:
        click = False
        print(xd, yd)