How to kill a WxPython application when user clicks a frame's close

前端 未结 4 528
情深已故
情深已故 2020-12-09 10:40

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 c

4条回答
  •  渐次进展
    2020-12-09 11:04

    One of the previous answers discusses using Destroy() rather than Close() in order to save user information before quitting the application. Here is some sample code asking the user if they intend to quit. If they respond with an affirmative, it "destroys" the frame and quits the application.

    # Bind our events from our menu item and from the close dialog 'x' on the frame
    def SetupEvents(self):
        self.Bind(wx.EVT_CLOSE, self.OnCloseFrame)
        self.Bind(wx.EVT_MENU, self.OnCloseFrame, self.fileMenuExitItem)
    
    
    # Destroys the main frame which quits the wxPython application
    def OnExitApp(self, event):
        self.Destroy()
    
    
    # Makes sure the user was intending to quit the application
    def OnCloseFrame(self, event):
        dialog = wx.MessageDialog(self, message = "Are you sure you want to quit?", caption = "Caption", style = wx.YES_NO, pos = wx.DefaultPosition)
        response = dialog.ShowModal()
    
        if (response == wx.ID_YES):
            self.OnExitApp(event)
        else:
            event.StopPropagation()
    

提交回复
热议问题