运行实时matplotlib draw()函数时,wxpython接口将无响应

2024-10-03 11:17:44 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我使用wxpython为一个程序制作一个GUI。我也在这个程序中嵌入了matplotlib图形。在

我的问题是,当我试图使用draw()连续更新绘图时,我的程序变得没有响应,但是matplotlib图形仍在更新。在

这是我的部分代码,以及我如何执行这些代码

def update(self, e):
    if self.liveMode:
        self.tune.plot();  # new plot
    self.canvas.draw();
    self.startLive(e);

def startLive(self, e):
    self.liveMode = False;
    refFreq = 500e6 / 80;
    qx = self.pv1.get();
    qy = self.pv2.get();
    self.tune.newWorkingpoint(refFreq/qx, refFreq/qy);
    self.tune.liveUpdate();
    time.sleep(0.3);
    self.update(e);

当在我的程序中按下'livemode'按钮时,将调用'update'方法。 我绘制所有的背景线,然后重复从其他地方获取新的点值,并将其绘制为图形上的一个圆,并使用draw()方法更新。当然,由于我基本上处于一个连续的循环中,在这个过程完成之前,没有其他任何东西可以工作。我的目标是停止程序的循环,因为程序停止时,我的目标就是停止绘图按钮。在

这附近有吗?我研究过线程,但我也遇到了同样的问题。在

编辑:---------------------------------------------------------------------------------------------------------我用this页面作为向导,使其正常工作。然而,几秒钟后,我得到这个致命的错误,我的程序崩溃。 'python2.7:X服务器上的致命IO错误11(资源暂时不可用):0.0。'

这是我实现的方法。当按下启动按钮时,“惊吓”启动,当按下停止按钮时,“停止现场”启动。一切都很好,matplotlib图也得到了正确的更新,而wxpythongui没有响应。在

问题是几秒钟后,我得到致命的错误,我的程序崩溃。我相信这很可能与画布.draw()方法更新线程内的matplotlib。有什么办法解决这个问题吗?在

^{pr2}$

---------------------------编辑--------------------------------- 如果有人感兴趣的话我解决了这个问题

在线程运行方法中,我删除了循环,只需要一个“更新”实例。更新后,wx.事件后调用,该方法在包含wx.应用程序它通过调用draw()来更新绘图。在此之后,线程将再次重新启动,并重复相同的进程。我的图形用户界面继续工作,绘图速度仍然非常快。重要方法如下:

在线程.开始()被调用,下面的执行器

    def run(self):
        refFreq = 500e6 / 80;
        self._tune.newWorkingpoint(refFreq / self._pv1.get(), refFreq /self._pv2.get());
        self._tune.liveUpdate();
        evt = CountEvent(myEVT_PLOT, -1);
        wx.PostEvent(self._parent, evt);

在wx.事件后在main中调用以下新方法wx.应用程序同学们

    def continueLive(self, evt):
        if self.livemode.get():
            self.canvas.draw();
            self.startLive(evt)

情节更新,整个过程重新开始。在

这种方法使图形用户界面保持响应,并且绘图速度不会减慢。在


Tags: 方法self程序图形绘图getmatplotlibdef
1条回答
网友
1楼 · 发布于 2024-10-03 11:17:44

我认为这可能是一个堆栈问题。更新循环是递归的,堆栈的大小是有限的(每个函数调用都被推送到堆栈中,并在函数返回后被移除)。在

您应该将其更改为迭代版本。在

def update(self, e):
    if self.liveMode:
        self.tune.plot();  # new plot
    self.canvas.draw();
    #self.startLive(e); # causes endless recursion

def startLive(self, e):
    while (self.isInLiveMode): # some flag here that gets set to False once the mode is deactivated
        self.liveMode = False;
        refFreq = 500e6 / 80;
        qx = self.pv1.get();
        qy = self.pv2.get();
        self.tune.newWorkingpoint(refFreq/qx, refFreq/qy);
        self.tune.liveUpdate();
        time.sleep(0.3);
        self.update(e);

嗯,我想self.Live模式应该是那面旗子。在

希望这有帮助,LG 丹尼尔

相关问题 更多 >