wxpython:应用程序在 window 关闭时不退出

wxpython: app not exiting when window is closed

我的应用程序在关闭我的应用程序的主程序后没有退出主循环 window。 为什么我关闭 window 后不打印 "finished"?

这是我的代码:

import wx
import numpy as np
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

class GraphFrame(wx.Frame):

    def __init__(self):
        self.displaySize = wx.DisplaySize() 
        wx.Frame.__init__(self, None, -1,
                 style = wx.DEFAULT_FRAME_STYLE,
                 size = (self.displaySize[0], self.displaySize[1]))
        self.threshold = 3000
        self.create_main_panel()
        self.draw_plot()

    def create_main_panel(self):
        self.panel = wx.Panel(self,-1, style = wx.SUNKEN_BORDER)
        self.fig = plt.figure()
        self.canvas = FigureCanvas(self.panel, -1, self.fig)

        self.panelsizer = wx.BoxSizer(wx.HORIZONTAL)
        self.panelsizer.Add(self.canvas, 1, wx.EXPAND)        
        self.panel.SetSizer(self.panelsizer)
        mainsizer = wx.BoxSizer(wx.VERTICAL)
        mainsizer.Add(self.panel, 1, wx.EXPAND )        
        self.SetSizerAndFit(mainsizer)

        self.init_plot()

    def init_plot(self):
        self.axes = self.fig.add_subplot(111)
        self.axes.set_axis_bgcolor('white')
        self.axes.set_title('TITLE', size=12)
        self.data = ['2000','2869','4694','2356','3600','1500']
        self.xmin = 0
        self.xmax = len(self.data)

    def draw_plot(self):
        self.plot_data = self.axes.plot(
          self.data, 
          linewidth=3,
          label = "plot1",
          marker = "o",
          markersize =7,
          )[0]
        self.plot_data.set_xdata(np.arange(len(self.data)))
        self.plot_data.set_ydata(np.array(self.data))
        thresholdplot = self.axes.plot([self.xmin,self.xmax], [self.threshold,self.threshold],"r--",label = "threshold",linewidth = 1)
        lg=self.axes.legend(loc="upper left", bbox_to_anchor=(1,1),ncol=1)
        self.canvas.draw()

if __name__ == "__main__":   
  app = wx.PySimpleApp()
  app.frame = GraphFrame()
  app.frame.Show()
  app.MainLoop()
  print "Finished"

是否需要单独处理close事件?我尝试处理 EVT_CLOSE 事件来破坏主机。但是还是没有任何反应。

我在 windows 7

上使用 wx 3.0 和 python 2.7

您调用了 matplotlib.pyplot 的恶魔,它有自己的事件循环。

我无法向您解释为什么它不起作用,但您可以按以下方式解决:

除了编写简单的脚本之外,还可以使用 matplotlib 的完全面向对象的接口(请参阅 matplotlib 上的 wxPython 示例)。

这可以按如下方式进行:

#import matplotlib.pyplot as plt
from matplotlib.figure import Figure

下面:

    self.fig = Figure() # instead of plt.figure()