Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8022745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:21:30+00:00 2026-06-04T22:21:30+00:00

The application is supposed to close when I click the close button for the

  • 0

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.

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()

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__()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:21:33+00:00Added an answer on June 4, 2026 at 10:21 pm

    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()
    

    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.

    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


    OP’s addition

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

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im writing an application that supposed to send coordinates in an SMS, but I've
my web application supposed to send email reminders in some point it will send
I'm building an application that supposed to store some images from user's Facebook accounts
I am writing an application that is supposed to fetch the source code of
I have an application that is supposed to aid my project in terms of
I have the sample console application which is supposed to be executed in the
I'm working on a C# application that is supposed to examine other C# executables
I am developing an OS X application that is supposed to take input from
I'm currently reviewing/refactoring a multithreaded application which is supposed to be multithreaded in order
I'm developing a new rails application which is supposed to be installed several times

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.