简体   繁体   中英

WxPython window not closing

I learnt how to detect window closing using WxPython but I have a problem. When I click 'X' on the top right corner I expect a dialog to appear saying 'Are you sure you want to exit?' And 2 buttons, Yes or no appear. That is fine. But when I click Yes, it is supposed to close but when I click No, it's supposed to not close. No is working fine but when I click yes, the dialog closes and opens again and I cannot close the main application...I had to use Task manager...What am I doing wrong? Please help me: Here is my close function:

def OnExit(self, event):
    
    if event.GetEventType() == wx.EVT_CLOSE.typeId:
        
        dial = wx.MessageDialog ( None, 'Are you sure you want to exit?', 'Exclamation',
        wx.YES_NO | wx.ICON_EXCLAMATION )
        
        if dial.ShowModal() == wx.ID_NO:
            
            pass
        
        else:
            self.Close()

Please help me! Any replies will be appreciated!!Thanks in advance!

You are, after all, calling self.Close() and therefore creating a new EVT_CLOSE event, to put you in an infinte loop.
And , because of the structure of your if statement, you can no longer close the program with File -> Quit , using the menu.
I'd suggest something along these lines.

    if event.GetEventType() == wx.EVT_CLOSE.typeId:
        
        dial = wx.MessageDialog ( self, 'Are you sure you want to exit?', 'Exclamation',
        wx.YES_NO | wx.ICON_EXCLAMATION )
        
        if dial.ShowModal() == wx.ID_NO:
            return
        
    self.Destroy()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM