【发布时间】:2012-08-08 11:57:07
【问题描述】:
我非常努力地理解这一点,感觉如此接近! 我有一个循环,我在其中创建了一些 wxpanels 并将它们附加到笔记本选项卡上。每个选项卡都包含一个图。最初对绘图的事件调用没有通过正确的绘图面板,因此事件处理函数报告了错误绘图的位置。我想我会用字典来解决这个问题:
self.plotdict ["plot" + str(plotcounter)] = wx.Panel(self, id=wx.ID_ANY)
然后我能够更改我的函数,使其传递 dict 键而不是 plot,无论如何我感觉更好。
self.plotdict ["plot" + str(plotcounter)].canvas.mpl_connect('motion_notify_event', lambda event: self.cbUpdateCursor(event, ("plot" + str(plotcounter))))
然而,即使这个循环已经运行了很多次,并且我在选项卡中有很多绘图,循环中的这一行总是将相同的键发送到函数 self.cbUpdateCursor,就好像它只使用函数调用创建的最后一个选项卡/绘图。
def cbUpdateCursor(self, event, plot):
if event.inaxes:
text = 'x = %5.4f, y = %5.4f' % (event.xdata, event.ydata)
print text
print plot
print self.plotdict[plot]
print id(self.plotdict [plot])
self.plotdict [plot].cursor_pos.SetLabel(text)
这意味着该函数的打印(仅用于测试)显示相同的绘图参考,无论哪个选项卡以及鼠标事件发生在哪个绘图上。
Print results for mouse over plot1
x = -127.8006, y = 135.9350
plot3
<wx._windows.Panel; proxy of <Swig Object of type 'wxPanel *' at 0x2f63758> >
52125616
Print results for mouse over plot2
x = -185.0618, y = 137.9096
plot3
<wx._windows.Panel; proxy of <Swig Object of type 'wxPanel *' at 0x2f63758> >
52125616
为什么我的画布上的鼠标事件会调用与绘图 3 相关的函数,而不管我的鼠标在哪个绘图上? (在这种情况下,情节 3 是最后一个创建的)。
【问题讨论】:
-
能否请您发布相关的
self.Bind()电话? -
我似乎没有进行与此事件句柄相关的绑定调用。
标签: python event-handling matplotlib wxpython