Re: wxPython, handling close events

"Matt S." <[email protected]>
Newsgroups gmane.comp.python.windows-ce
Message-ID <[email protected]>
Here's some sample code that highlights my application closing problem.  If
a user uses the Quit button, no problem.  If on the other hand they close
the frame, hay mucho problemas.

I've included some comments and commented out some code that can be used to
alter the applications behavior.  Ideally, I want the user to have to
confirm that they want to quit before the program ever begins to be
destroyed.

Thanks!
Matt




---------- Forwarded message ----------
> From: "Matt S." <[email protected]>
> To: [email protected]
> Date: Wed, 21 Feb 2007 21:12:08 -0800
> Subject: [PythonCE] wxPython, handling close events
> I have a a wxPython application on the PPC 2003, Python 2.4, etc...
>
> I can gracefully exit on win32 from the frame close button or any button
> within the frame.  My trouble arises on the PPC because the frame widgets
> (esp. close button) seem to not be associated with wx as much as the system
> and don't clean up properly.  When I tap the close circle-X, the top level
> window goes away but the Python CE window remains.  Then if I want to
> delete/replace the program my request gets refused because the original file
> is in use.  Furthermore, if I start up the file again it will run but after
> loading up, the top level window is the old tlw.
>
> I've been experimenting with binding close events to a pre-exit function.
> I want to either kick up a message dialog to make sure the user wants to
> exit or just veto (event.Veto()) the frame level close button event.
>
> self.top_window.Bind(wx.EVT_CLOSE, self.PreExit)
>
> I've also fooled around with the following at the application level,
>
> self.Bind(wx.EVT_QUERY_END_SESSION, self.OnExit)
> self.Bind(wx.EVT_END_SESSION , self.OnExit)
>
> I'm afraid the frame close button event I'm trying to handle is some sort
> of hybrid.
>
> I won't describe it here but I also have a few other issues:
> 1) when the PPC goes into sleep mode bad things happen to my app
> 2) I've tried to use PocketConsole and the kill utility to find a
> resulting hung python program file but I don't see the file as a process.
> Python.exe gets listed as a process but I'm not sure I've successfully
> ended it.  I assume that if I do, the hung python program will get cleaned
> up.
> 3) Basic PPC os questions:
> -If a python file that is being interpreted is not a process, what is it?
> -Anyone know how to clean up one of the files when it's not ended
> properly?
>
> If no one has a quick comment that breaks the dam, tomorrow I'll put
> together a simple example of my problem and post it for interrogation.
>
> Thanks!
> Matt
>
> Lastly, please let me know if you think this subject should be posted to a
> wxPython list (and which one).  Maybe a wxPythonCE list would be helpful in
> the near future?
>

_______________________________________________
PythonCE mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pythonce
simple_frame.py (text/x-python, 4.9 KB)
import sys #, os, os.path, time

if sys.platform == 'win32':
    pass
else:
    # This gets rid of annoying pin/progress wheel. ala Luke.
    try:
        import _pcceshell_support
    except ImportError:
        pass
    else:
        _pcceshell_support.Busy(0)    


import wx
import traceback 
#import glob
#import datetime

class myButton(wx.Panel):

    def __init__(self, parent, id=-1, label=None, size=(-1,-1), style=wx.BU_EXACTFIT , button_color='Navy'):
       
        wx.Panel.__init__(self, parent, id=-1, style=wx.NO_BORDER)        
        buttonSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.aButton = wx.Button(self, id=wx.ID_ANY, size=size, label=label, style=style)#, size=(-1,20))
        self.aButton.SetBackgroundColour(button_color)
        self.aButton.SetForegroundColour('White')   

        buttonSizer.Add(self.aButton)
        self.SetSizer(buttonSizer)

class FrameWithForms(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(FrameWithForms, self).__init__(*args, **kwargs)
                
#        self.panel = wx.Panel(self, -1)#, size=(100,100))
#        self.panel.SetBackgroundColour('green')
#        abutton = myButton(self.panel, size=(50,50), label='Quit')
#        self.panel.SetSizer(mySizer)

        wx.Panel(self, -1, (100,100))
        self.SetBackgroundColour('green')
        
        abutton = myButton(self, size=(50,50), label='Quit')        
        
        mySizer = wx.BoxSizer(wx.VERTICAL)
        mySizer.Add(abutton)
        
        self.SetSizer(mySizer)
        
## Binding may prevent event from being received by application?
        self.Bind(wx.EVT_BUTTON, self.quit_button_handler)
#        abutton.Bind(wx.EVT_BUTTON, self.quit_button_handler) 
## bind direct to abutton might prevent event from being recieved by parents

## to confirm close.
        self.Bind(wx.EVT_CLOSE, self.quit_button_handler) 
## no confirmation required.
#        self.Bind(wx.EVT_CLOSE, self.quit_now)        

    def quit_now(self, event=None):
        self.Destroy()

    def quit_button_handler(self, event=None):
        retCode = self.ConfirmDialog('Are you sure you want to exit?', 'Warning')
#        dialog.ShowModal()
        print retCode
        if retCode == wx.ID_YES:
            self.Destroy()
        else:
            print 'skipping quit'

    def ConfirmDialog(self, text, title):
        dialog = wx.MessageDialog(self, text, title, wx.YES_NO | wx.ICON_QUESTION)
        retCode = dialog.ShowModal()
        return retCode


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

    def OnInit(self):
        # use this to pass args to frame
        # but don't duplicate in argsin 
        kwargs = {'size':(270,350),'name':'ceSCAT Notebook'}
#        self.mainframe = FrameWithForms(None, **kwargs)
#        self.mainframe = FrameWithForms(None, -1, 'eSCAT', style=wx.STAY_ON_TOP, **kwargs)
        self.mainframe = FrameWithForms(None, -1, 'eSCAT', **kwargs)
        self.mainframe.Show()
        
        self.SetTopWindow(self.mainframe)
        
        self.top_window = self.GetTopWindow()

##        self.top_window.ShowFullScreen(True, wx.FULLSCREEN_ALL)# Only works on Windows. no user32.dll

#        self.top_window.Bind(wx.EVT_CLOSE, self.top_window.quit_button_handler)

#        self.top_window.Bind(wx.EVT_CLOSE, self.PreExit)
        
#        # This event handler help when forces external to the 
#        # app are going to kill the program (ie. shutdown)
#        # self.OnExit is just my hook to not handle this yet
#        # TODO: Write a App End Session handler
##        self.Bind(wx.EVT_QUERY_END_SESSION, self.PreExit)
#        self.Bind(wx.EVT_QUERY_END_SESSION, self.OnExit)
        
        return True   
#
#    def EndExit(self, event):
##        event.Skip()
#        dialog = wx.MessageDialog(self.top_window, 'End Event!', 'Warning', wx.OK | wx.ICON_EXCLAMATION)
#        dialog.ShowModal()
#    
#    def PreExit(self, event):
#        pass
###        event.Skip()
##        dialog = wx.MessageDialog(self.top_window, 'Close event: exit using the QUIT button!', 'Warning', wx.OK | wx.ICON_EXCLAMATION)
##        dialog.ShowModal()
###        print event.CanVeto()
###        event.Veto()

    def OnExit(self):        
#        self.Destroy() # Not necessary?
    
        print 'OnExit'


if __name__ == '__main__':


    try: 
        # For stdout, go to \Temp\app_output.txt
        app = MyApp(redirect=True, filename='app_output.txt')

#        app = MyApp(0) # No redirect, etc.
#        app.SetExitOnFrameDelete(True) # This is a default for apps
        app.MainLoop()

    except:
        traceback.print_exc()
#        app.Destroy()
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.