Python:单击鼠标返回坐标信息

Python:单击鼠标返回坐标信息,python,matplotlib,user-interaction,Python,Matplotlib,User Interaction,我想用python显示一个图像,并允许用户点击一个特定的像素。然后我想使用x和y坐标来执行进一步的计算 到目前为止,我一直在使用事件选择器: def onpick1(event): artist = event.artist if isinstance(artist, AxesImage): mouseevent = event.mouseevent x = mouseevent.xdata y = mouseevent.ydata

我想用python显示一个图像,并允许用户点击一个特定的像素。然后我想使用x和y坐标来执行进一步的计算

到目前为止,我一直在使用事件选择器:

def onpick1(event):
    artist = event.artist
    if isinstance(artist, AxesImage):
        mouseevent = event.mouseevent
        x = mouseevent.xdata
        y = mouseevent.ydata
        print x,y

xaxis = frame.shape[1]
yaxis = frame.shape[0]
fig = plt.figure(figsize=(6,9))
ax = fig.add_subplot(111)
line, = [ax.imshow(frame[::-1,:], cmap='jet', extent=(0,xaxis,0,yaxis), picker=5)]
fig.canvas.mpl_connect('pick_event', onpick1)
plt.show()
现在我非常希望函数onpick1()返回x和y,这样我可以在
plt.show()之后使用它来执行进一步的计算


有什么建议吗?

GUI编程的一个很好的教训是面向对象。现在的问题是,您有一个异步回调,并且希望保留其值。你应该考虑把所有东西打包在一起,比如:

class MyClickableImage(object):
    def __init__(self,frame):
        self.x = None
        self.y = None
        self.frame = frame
        self.fig = plt.figure(figsize=(6,9))
        self.ax = self.fig.add_subplot(111)
        xaxis = self.frame.shape[1]
        yaxis = self.frame.shape[0]
        self.im = ax.imshow(self.frame[::-1,:], 
                  cmap='jet', extent=(0,xaxis,0,yaxis), 
                  picker=5)
        self.fig.canvas.mpl_connect('pick_event', self.onpick1)
        plt.show()

    # some other associated methods go here...

    def onpick1(self,event):
        artist = event.artist
        if isinstance(artist, AxesImage):
            mouseevent = event.mouseevent
            self.x = mouseevent.xdata
            self.y = mouseevent.ydata
现在,当您单击一个点时,它将设置类的
x
y
属性。但是,如果要使用
x
y
执行计算,只需使用
onpick1
方法执行这些计算即可