Handling wx.EVT_CLOSE

Hello all,

I read through the archives and saw some questions about this event but I am still having some problems with it. I don’t know if this is a GNOME thing or I am doing something wrong. I am trying to run this small piece of code and when I close the application on Linux it prompts with “The window SimpleFrame is not responding” and asks me if I want to Force Quit or Cancel, however the window still responds if I click Cancel.

This does not happen on Windows and am wondering if anyone else has come accross a problem like this. If so is there a work-around so this doesn’t happen everytime I close the application.

By the way the flavor of Linux is RedHat.

import wx

class AppInit(wx.App):

def __init__(self, redirect=False, filename=None, useBestVisual=False, clearSigInit=True):
    wx.App.__init__(self,redirect,filename,useBestVisual,clearSigInit)

def OnInit(self):
    Frame = MyFrame()
    Frame.Show(True)
    self.SetTopWindow(Frame)
    Frame.CentreOnScreen()
    return True

class MyFrame(wx.Frame):

def __init__(self, parent=None, id=-1, title="SimpleFrame", pos=wx.DefaultPosition, size=(800,800),
             style=wx.DEFAULT_FRAME_STYLE | wx.SUNKEN_BORDER | wx.CLIP_CHILDREN):

    wx.Frame.__init__(self, parent, id, title, pos, size, style)

    self.Bind(wx.EVT_CLOSE, self.OnAppClose)

def OnAppClose(self, evt):
    msg = "Hold on there a minute"
    dlg = wx.MessageDialog(None, msg, "Wait ...",
        wx.YES_NO | wx.ICON_EXCLAMATION)
   
    if dlg.ShowModal() == wx.ID_YES:
        self.Destroy()
    else:
        return
   
    dlg.Destroy()

App = AppInit(0)
App.MainLoop()

Thanks
Scott

Scott,

Hello all,

I read through the archives and saw some questions about this event but I am still having some problems with it. I don't know if this is a GNOME thing or I am doing something wrong. I am trying to run this small piece of code and when I close the application on Linux it prompts with "The window SimpleFrame is not responding" and asks me if I want to Force Quit or Cancel, however the window still responds if I click Cancel.

This does not happen on Windows and am wondering if anyone else has come accross a problem like this. If so is there a work-around so this doesn't happen everytime I close the application.

By the way the flavor of Linux is RedHat.

import wx

class AppInit(wx.App):

    def __init__(self, redirect=False, filename=None, useBestVisual=False, clearSigInit=True):
        wx.App.__init__(self,redirect,filename,useBestVisual,clearSigInit)

    def OnInit(self):
        Frame = MyFrame()
        Frame.Show(True)
        self.SetTopWindow(Frame)
        Frame.CentreOnScreen()
        return True

class MyFrame(wx.Frame):

    def __init__(self, parent=None, id=-1, title="SimpleFrame", pos=wx.DefaultPosition, size=(800,800),
                 style=wx.DEFAULT_FRAME_STYLE | wx.SUNKEN_BORDER | wx.CLIP_CHILDREN):

        wx.Frame.__init__(self, parent, id, title, pos, size, style)

        self.Bind(wx.EVT_CLOSE, self.OnAppClose)
       def OnAppClose(self, evt):
        msg = "Hold on there a minute"
        dlg = wx.MessageDialog(None, msg, "Wait ...",
            wx.YES_NO | wx.ICON_EXCLAMATION)
               if dlg.ShowModal() == wx.ID_YES:
            self.Destroy()
        else:
            return
               dlg.Destroy()

App = AppInit(0)
App.MainLoop()

Thanks
Scott

Hmmm...I tried your code on Ubuntu 8.10 and it worked fine for me. Are you using the latest wxPython? What about your Python version?

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

Mike,

My version of Python is 2.6 and wxPython is 2.8.8.1 I see there is more recent version out there. I have also run this on Ubuntu and it works fine. I am wondering if it is something with RedHat. I am just curious if there is something unusual about my code.

Thanks again for your help.

Scott

Scott Hall wrote:

Mike,

My version of Python is 2.6 and wxPython is 2.8.8.1 I see there is more recent version out there. I have also run this on Ubuntu and it works fine. I am wondering if it is something with RedHat. I am just curious if there is something unusual about my code.

Thanks again for your help.

Scott

You seem to be catching and handling the close event the same way I have in the past, so I'm guessing it is a "Red Hat" thing, but I don't really know what to tell you to try to track it down.

Mike

Hello Scott,

I don't have a Linux version to try but I looked at your code.

May I suggest you try out this little change:

[...snip...]
        if dlg.ShowModal() == wx.ID_YES:
            self.Destroy()
        dlg.Destroy()
        return

App = AppInit(False)
[...snip...]

The difference with yours is that it closes the frame (self.Destroy)
and the dialogue (dlg.Destroy) and then returns.

Should this work, maybe there could be some running conditions that
make what you see happening. Just guessing here.

Regards, Rene

···

On Thu, Jan 15, 2009 at 8:42 PM, Scott Hall <smhall316@gmail.com> wrote:

Hello all,

I read through the archives and saw some questions about this event but I am
still having some problems with it. I don't know if this is a GNOME thing
or I am doing something wrong. I am trying to run this small piece of code
and when I close the application on Linux it prompts with "The window
SimpleFrame is not responding" and asks me if I want to Force Quit or
Cancel, however the window still responds if I click Cancel.

This does not happen on Windows and am wondering if anyone else has come
accross a problem like this. If so is there a work-around so this doesn't
happen everytime I close the application.

By the way the flavor of Linux is RedHat.

import wx

class AppInit(wx.App):

    def __init__(self, redirect=False, filename=None, useBestVisual=False,
clearSigInit=True):
        wx.App.__init__(self,redirect,filename,useBestVisual,clearSigInit)

    def OnInit(self):
        Frame = MyFrame()
        Frame.Show(True)
        self.SetTopWindow(Frame)
        Frame.CentreOnScreen()
        return True

class MyFrame(wx.Frame):

    def __init__(self, parent=None, id=-1, title="SimpleFrame",
pos=wx.DefaultPosition, size=(800,800),
                 style=wx.DEFAULT_FRAME_STYLE | wx.SUNKEN_BORDER |
wx.CLIP_CHILDREN):

        wx.Frame.__init__(self, parent, id, title, pos, size, style)

        self.Bind(wx.EVT_CLOSE, self.OnAppClose)

    def OnAppClose(self, evt):
        msg = "Hold on there a minute"
        dlg = wx.MessageDialog(None, msg, "Wait ...",
            wx.YES_NO | wx.ICON_EXCLAMATION)

        if dlg.ShowModal() == wx.ID_YES:
            self.Destroy()
        else:
            return

        dlg.Destroy()

App = AppInit(0)
App.MainLoop()

Thanks
Scott

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Rene,

Thanks for the suggestion. I tried it out but that frustrating window-not-responding dialog keeps occurring, that is unless I quickly make a selection on my dialog box.

Scott

Mike Driscoll wrote:

Scott Hall wrote:

Mike,

My version of Python is 2.6 and wxPython is 2.8.8.1 I see there is more recent version out there. I have also run this on Ubuntu and it works fine. I am wondering if it is something with RedHat. I am just curious if there is something unusual about my code.

Thanks again for your help.

Scott

You seem to be catching and handling the close event the same way I have in the past, so I'm guessing it is a "Red Hat" thing, but I don't really know what to tell you to try to track it down.

Mike
____________

Just for kicks, try destroying the dialog BEFORE you destroy the frame.

Mike

Scott,

When nothing seems to work I use WingWare101 and step through the
code. Usually I find what is happening or at least I'm hinted towards
some solution. It is a good tool for a newbie like myself.

If I'm getting desperate, then I go back to the heart of Python:
Guido's work, more precisely his IDLE. It is incredible how this IDLE
is robust and resilient and gives a lot of information.

Hope you'll find what is happening.
Cheers, Rene

···

On Thu, Jan 15, 2009 at 9:15 PM, Scott Hall <smhall316@gmail.com> wrote:

Rene,

Thanks for the suggestion. I tried it out but that frustrating
window-not-responding dialog keeps occurring, that is unless I quickly make
a selection on my dialog box.

Scott

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Rene Heymans wrote:

Scott,

When nothing seems to work I use WingWare101 and step through the
code. Usually I find what is happening or at least I'm hinted towards
some solution. It is a good tool for a newbie like myself.

If I'm getting desperate, then I go back to the heart of Python:
Guido's work, more precisely his IDLE. It is incredible how this IDLE
is robust and resilient and gives a lot of information.

Hope you'll find what is happening.
Cheers, Rene

Wingware is cool...although I have noticed it will throw spurious warning in my wx programs that I end up having to ignore...and IDLE won't always run a wx program since their main loops can conflict with each other. However, I can usually get it to work just fine.

Mike

···

On Thu, Jan 15, 2009 at 9:15 PM, Scott Hall <smhall316@gmail.com> wrote:
  

Rene,

Thanks for the suggestion. I tried it out but that frustrating
window-not-responding dialog keeps occurring, that is unless I quickly make
a selection on my dialog box.

Scott

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Scott,

Scott Hall wrote:

Hello all,

...

      def OnAppClose(self, evt):
        msg = "Hold on there a minute"
        dlg = wx.MessageDialog(None, msg, "Wait ...",
            wx.YES_NO | wx.ICON_EXCLAMATION)
               if dlg.ShowModal() == wx.ID_YES:
            self.Destroy()
        else:
            return
               dlg.Destroy()

I would change above to:
def OnAppClose(self, evt):
        msg = "Hold on there a minute"
        dlg = wx.MessageDialog(None, msg, "Wait ...",
            wx.YES_NO | wx.ICON_EXCLAMATION)
        try:
            if not dlg.ShowModal() == wx.ID_YES:
               return
        finally: dlg.Destroy()

       evt.Skip()

IIRC it is recommended to use try/finally on dialogs to ensure that they always get destroyed.

Just doing evt.Skip will call the standard handler which should then shut down your app.

Werner