wxpython:窗口关闭时应用程序不退出 [英] wxpython: app not exiting when window is closed

查看:89
本文介绍了wxpython:窗口关闭时应用程序不退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关闭我的应用程序的主窗口后,我的应用程序没有退出主循环.关闭窗口后为什么不打印完成"?

My app is not exiting mainloop after closing my application's main window. why is it not printing "finished" after I close the window?

这是我的代码:

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"

我需要单独处理关闭事件吗?我尝试处理EVT_CLOSE事件以破坏主机.但是仍然没有任何反应.

Do I need to handle the close event separately? I tried handling the EVT_CLOSE event to destroy the main frame. But still nothing happens.

我在Windows 7上将wx 3.0与python 2.7一起使用

Iam using wx 3.0 with python 2.7 on windows 7

推荐答案

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

You invoked the demons of matplotlib.pyplot which has its own eventloop.

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

I can not explain to you why exactly it does not work, but you can resolve it as follows:

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

Use the fully object-oriented interface of matplotlib when doing more than writing simple scripts (see the wxPython examples on matplotlib).

可以这样做:

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

下方:

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

这篇关于wxpython:窗口关闭时应用程序不退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆