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方法执行这些计算。在