Wx Matplotlib事件处理

贾斯汀·史密斯

我正在使用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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Matplotlib画布未嵌入wx面板

来自分类Dev

wx.Panel中的matplotlib.pyplot

来自分类Dev

手动触发wx.EVT_SIZE事件

来自分类Dev

Matplotlib FuncAnimation 不更新嵌入在 wx 面板中的绘图

来自分类Dev

通过单击另一个类中的按钮从面板内的事件处理程序关闭 wx.Dialog

来自分类Dev

wxpython wx.Slider:仅当用户暂停预定时间后如何触发事件

来自分类Dev

使用wx.toolbar在wxPython中单击后是否可以执行事件?

来自分类Dev

是否可以使用Python WX向导调用按钮单击事件?

来自分类Dev

wxpython wx.Slider:仅当用户暂停预定时间后如何触发事件

来自分类Dev

Matplotlib事件处理,无需使用show()

来自分类Dev

Matplotlib事件处理,无需使用show()

来自分类Dev

如何在wx.notebook中调整多个画布的大小,浮点大小和重叠问题(WxPython,Matplotlib)

来自分类Dev

wxPython wx.CallAfter()

来自分类Dev

Matplotlib事件处理:何时发送matplotlib事件以及何时发送Qt事件

来自分类Dev

在wx.wizard中使用wx.gauge

来自分类Dev

在wx.Panel中的wx.Frame中使用变量

来自分类Dev

wxPython wx.ScrolledWindow插入wx.Panel

来自分类Dev

wxPython wx.Frame和wx.Dialog效果

来自分类Dev

如何在wxPython中使用matplotlib的事件处理?

来自分类Dev

锁定wx.stc.StyledTextCtrl

来自分类Dev

WX StaticBoxSizer / Sizer边框宽度

来自分类Dev

继承wx.grid.GridCellTextEditor

来自分类Dev

WxPython:导入wx.lib

来自分类Dev

在 wx.Panel 中滚动

来自分类Dev

wx python 图像质量下降

来自分类Dev

将 wx.grid 传递给 wx.frame WX.python

来自分类Dev

wxPython wx.lib.plot.PlotCanvas错误

来自分类Dev

wx.BoxSizer以垂直和水平居中

来自分类Dev

Python wx.listctrl滚动位置

Related 相关文章

热门标签

归档