Q:

Assign coordinates of an image to variables on a mouse click

The following code gives me the x and y coordinates of an image on a mouse click event.   I want to assign the x and y coordinates to global variables x and y instead of printing them and then close the image.   How do I do this please?    
%matplotlib tk
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

fig = plt.figure(figsize=(20,30))

img=mpimg.imread(workingimage)

def onclick(event):
    ix, iy = event.xdata, event.ydata
    print(ix, iy)

cid = fig.canvas.mpl_connect('button_press_event', onclick)

imgplot = plt.imshow(img)
plt.show()

A:

You could assign the values to 2 global variables and call plt.close():
%matplotlib tk
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

fig = plt.figure(figsize=(20,30))

img = mpimg.imread(workingimage)

def onclick(event):
    global clicked_on_x, clicked_on_y
    clicked_on_x, clicked_on_y = event.xdata, event.ydata
    plt.close()

cid = fig.canvas.mpl_connect('button_press_event', onclick)

imgplot = plt.imshow(img)
plt.show()
print("clicked on position", clicked_on_x, clicked_on_y)

# rest of code

