Wxpython + matplotlib:关闭图形挂起python



这个python程序在wxpython窗口中绘制图形。它在Enthought Python Distribution 7.3-2中运行良好,但是当我关闭图形时,Python继续运行并且不退出。

我错过了什么?当我创建不使用matplotlib的wxpython gui时,它们会在我关闭窗口后正确退出。我猜wxpython中一定有我不理解的消息循环。

# adapted from:
# http://wiki.wxpython.org/Getting%20Started
# http://www.cs.colorado.edu/~kena/classes/5448/s11/presentations/pearse.pdf
import wx
import pylab as pl
import matplotlib
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
class GUIPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.parent = parent
        # create some sizers
        sizer = wx.BoxSizer(wx.VERTICAL)
        # A button
        self.button =wx.Button(self, label="Tada!")
        self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
        # put up a figure
        self.figure = pl.figure()
        self.axes = self.drawplot(self.figure)
        self.canvas = FigureCanvas(self, -1, self.figure)
        sizer.Add(self.canvas, 0, wx.ALIGN_CENTER|wx.ALL)
        sizer.Add(self.button, 0, wx.ALIGN_CENTER|wx.ALL)
        self.SetSizerAndFit(sizer)  
    def log(self, fmt, *args):
        print (fmt % args)
    def OnClick(self,event):
        self.log("button clicked, id#%dn", event.GetId())
    def drawplot(self, fig):
        ax = fig.add_subplot(1,1,1)
        t = pl.arange(0,1,0.001)
        ax.plot(t,t*t)
        ax.grid()
        return ax
app = wx.App(False)
frame = wx.Frame(None)
panel = GUIPanel(frame)
frame.Fit()
frame.Center()
frame.Show()
app.MainLoop()

嗯。根据tcaswell评论中的讨论,我更改了

self.figure = pl.figure()

self.figure = matplotlib.figure.Figure()

,它解决了这个问题。(当我关闭窗口时python退出)

最新更新