当用户单击框架的关闭时如何终止 WxPython 应用程序 [英] How to kill a WxPython application when user clicks a frame's close

查看:31
本文介绍了当用户单击框架的关闭时如何终止 WxPython 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击主框架的关闭按钮时,应用程序应该关闭.但是我实现它的方式,当我单击按钮时,它会以 Segmentation fault 退出.

The application is supposed to close when I click the close button for the main frame. But the way I implemented it, it quits with a Segmentation fault when I click the button.

我担心程序的安全关闭,因为我稍后需要将内容保存到磁盘.

I worry about safe shutdown of the program, because I will need to persist stuff to disk later.

通过关闭按钮终止 WxPython 应用程序的适当的非暴力方式是什么?

What is the proper non-violent way to terminate a WxPython application though a close button?

这是我实现的程序的主"循环:

Here is the "main" loop of the program I implemented:

if __name__ == "__main__":
    app = wx.App(False)
    mf = MainFrame(None, title='Spectrum Checker') #subclasses frame
    mf.register_close_callback( app.Destroy) #what is the apt func?
    app.MainLoop()

这里是如何在MainFrame中实现回调:

Here is how it is implemented the callback within the MainFrame:

def __init__(self, parent, title):
    ...
    self.Bind(wx.EVT_CLOSE, self._when_closed)

...

def _when_closed(self, event):
if self.__close_callback__:
    self.__close_callback__()

推荐答案

这是关闭框架的正常方法:

Here's the normal method of closing a frame:

import wx

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Close Me")
        panel = wx.Panel(self)

        closeBtn = wx.Button(panel, label="Close")
        closeBtn.Bind(wx.EVT_BUTTON, self.onClose)

    #----------------------------------------------------------------------
    def onClose(self, event):
        """"""
        self.Close()

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

现在,如果您绑定到 wx.EVT_CLOSE,那么您将陷入无限循环,直到出现段错误.绑定到 EVT_CLOSE 的主要原因是您可以捕获关闭事件并要求用户保存他们的信息.如果你想这样做,那么你需要使用 self.Destroy 而不是self.Close",这样你就不会继续触发那个事件.

Now if you have a binding to wx.EVT_CLOSE, then you'll end up in an infinite loop until you get a segfault. The main reason to bind to EVT_CLOSE is so you can catch the close event and ask the user to save their information. If you want to do that, then you need to use self.Destroy instead of "self.Close" so you don't keep firing that event.

正如已经提到的,在您的关闭处理程序中,您需要确保结束线程、销毁任务栏图标、关闭打开的文件等,以免挂起任何事情.另见:http://wxpython.org/docs/api/wx.CloseEvent-类.html

As has already been mentioned, in your close handler you need to make sure you end threads, destroy taskbar icons, close open files, etc so nothing hangs. See also: http://wxpython.org/docs/api/wx.CloseEvent-class.html

我遇到了两个问题,感谢所有三个答案帮助我找到它们:

I was facing two issues, I thank all three answers for helping me find them:

首先,框架不响应self.Close()self.Destroy(),因为它有一个属性self.stuff> 有一个正在运行的线程.必须先关闭此线程.

First, the frame does not respond to self.Close() or self.Destroy() because it has an attribute self.stuff that has a running thread. This thread must be closed first.

其次,self.Close() 在响应关闭事件并在调用时导致无限递归的处理程序中.这会导致运行时错误(分段错误、超出递归深度).解决方案是使用 self.Destroy() 代替.

Second, self.Close() in a handler that responds to close events and causes an infinite recursion when called. This causes a runtime error (segmentation fault, recursion depth exceeded). The solution is to use self.Destroy() instead.

这篇关于当用户单击框架的关闭时如何终止 WxPython 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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