我正在使用WX-MPL应用程序,以根据用户输入显示和交互处理数据。我在设置MPL鼠标事件以在WX应用程序中工作时遇到困难。
目标是要有一系列可编辑的垂直线,这些线描述了在较早处理中已确定的要素的开始时间和结束时间。用户应该能够沿x轴拖动线条,删除不需要的线条并插入新的线条。
到目前为止,我已经能够使用标准MPL图(我认为是Tk)来实现此功能。当我尝试将这些类集成到我的应用程序中时,事件处理会误入歧途,并且无法与线对象进行交互或创建新的线对象。因此,我备份了一个步骤,并一直试图在工作示例中增加复杂性,以确定问题出在哪里。
当将工作示例作为简单的WX应用程序运行时,NewVLineListener类似乎不再接收button_press_events。以下是与我遇到的特定问题相关的代码。
这是我第一次处理鼠标事件,但是我肯定会丢失一些东西。任何建议将不胜感激。
另外,我正在运行WX 2.8.12和MPL 1.1.1rc。
from matplotlib.figure import Figure
import wx
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
class NewVLineListener:
global castLog, count, dvls, ax1
lock = None # only one can be animated at a time
def __init__(self, fig):
self.fig = fig
def connect(self):
'connect to all the events we need'
self.cidpressright = self.fig.canvas.mpl_connect(
'button_press_event', self.on_press_right)
def on_press_right(self, event):
global count, castLog, dvls, ax1
print event.button
if event.button != 3: return
tag = 'Begin'
# Increase castLog Key to accomodate new Vline
count += 1
print 'count: ', count
# Set castLog values to x-value of triggering event
castLog[str(count)] = { tag:event.xdata }
# Spawn a new DraggableVline instance
color_map = {'Begin':'g', 'End':'r'}
dvl = DraggableVline(self.fig, ax1.axvline(x=castLog[str(count)][tag], linewidth=2, color=color_map[tag]), count, tag)
dvl.connect()
dvls.append(dvl)
canvas = self.fig.canvas
canvas.draw()
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY)
global castLog, count, ax1, dvls
fig = Figure()
canvas = FigureCanvasWxAgg(self, -1, fig)
ax1 = fig.add_subplot(111)
# Create empty dict that will hold new V-line Data
castLog = { }
dvls = [ ]
count = 0
# Instantiate New Vline Listener
NVL = NewVLineListener(fig)
NVL.connect()
if __name__ == '__main__':
app = wx.PySimpleApp()
app.frame = MainFrame()
app.frame.Show()
app.MainLoop()
我得到了它!
我认为对NewVlineListener对象的引用正在被垃圾收集,因此事件从未被接收到。通过将NVL对象引用添加到可拖动的vline对象的引用数组,它可以粘住并接收预期的事件。
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY)
global castLog, count, ax1, dvls
fig = Figure()
canvas = FigureCanvasWxAgg(self, -1, fig)
ax1 = fig.add_subplot(111)
# Create empty dict that will hold new V-line Data
castLog = { }
dvls = [ ]
count = 0
# Instantiate New Vline Listener
NVL = NewVLineListener(fig)
NVL.connect()
dvls.append(NVL) # This keeps NVL reference from being garbage collected?
我仍然发现有趣的是,命名NewVlineListener对象不足以保持引用不变,并且它可以与pyplot一起正常工作,但现在可以与wx一起工作,但这似乎可行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句