关闭绘图窗口后,进程继续运行



我是matplotlib的新手,遇到了一些困难。从用户的角度来看,一切都很好。我有一个带按钮的窗口。按下按钮,一个新的窗口弹出显示的情节。后玩我用它点击X来关闭这个窗口。然后再次点击X,用按钮关闭窗口。但是我并没有回到一个自由提示符。主窗口被锁定,仍在后台运行。

下面是代码的重要部分:

import matplotlib
import matplotlib.pyplot as Plt
from matplotlib.figure import Figure
class MainPanel(wx.Panel):
    def __init__(self, parent): #=None
        wx.Panel.__init__(self, parent)
                       .... wxpython stuff goes here....
              self.btn1.Bind(wx.EVT_BUTTON, self.cplot)
                       ....
    def cplot(self, event):
        self.new = NewWindow(self)
        self.new.cidadeplot(self)
        self.new.Show()
        return

class NewWindow(wx.Frame):
    def __init__(self,event):
        wx.Frame.__init__(self, None, -1, 'Plot', size=(556, 618))
        wx.Frame.CenterOnScreen(self)
        self.Bind(wx.EVT_CLOSE, self.OnClose)

    def OnClose(self,event):
        self.Destroy()

    def cidplot(self,event):

        self.fig = Figure()
        self.axes = self.fig.add_subplot(111)
        self.canvas = FigCanvas(self, -1, self.fig)
        self.axes.set_ylabel("Parts")
        self.axes.set_ylim(0,100)
        self.axes.grid(True)
        ....more axes settings...
       bars = self.axes.bar(left=self.ii, height=self.tt, width=0.2, align='center', alpha=0.8)
        ....
        self.canvas.draw()
        Plt.savefig("Parts.pdf", dpi=300)

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title = "System Test")
        self.SetSize((556, 618))
        panel = MainPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()

我认为问题出在自己身上。fig = Figure()(如果我使用Plt.figure()也会发生同样的情况)

当我创建一个新窗口或在我的matplotlib部分时,我做错了什么?有更好的方法吗?

提前感谢。

不要导入pyplot,它有自己的包装来处理管理图形gui框架(除了状态机接口),这是pyplot的重点。为了做到这一点,它启动了自己的主循环,这可能是你搞砸的地方。

确保你看了这里的例子

使用此函数OnClose:

def onClose(self, event):
    """"""
    self.Close(True)

Tcaswell是对的。

我删除了所有对pyplot的引用。保存文件正在锁定应用程序。将所有更改为self.axes.XXX和salt .fig.savefig(xxx),并成功了。

谢谢。

最新更新