[Pythonmac-SIG] Closing windows on quit

Just van Rossum just@letterror.com
Tue, 15 Aug 2000 09:44:57 +0100


At 10:40 PM -0400 14-08-2000, Gordon Worley wrote:
>I'm trying to impliment in my program a feature where, on quit,
>rather than just forcing the windows closed and not checking to see
>if they have unsaved data (which, I take it, means that my windows'
>close functions are being overrided or not called in some way).  My
>original plan was to modify the _quit() function in my application
>object, making it close the Win.FrontWindow() until it gets an error
>(because there are no more windows), but this resulted in an
>AttributeError (the offending line of code was, and I quote,
>'Win.FrontWindow().close()').  There is, in fact, a close() function
>in all of the windows opened, so either I am somehow not accessing a
>window (or one not created by my code) or FrontWindow() doesn't do
>what I think it does (return the front most window).  Is it possible
>that it wouldn't recoginize my windows as windows (they inherit from
>W.Window, in case that might matter)?

Well, there's about three kinds of windows involved: the builtin Window
type (which is what FrontWindow() returns, FrameWork.Window and W.Window.
The latter is a subclass of the one in FrameWork. The application object
has a dictionary (app._windows) that maps builtin windows to their Python
conterparts. The builtin one doesn't have a close method; use the
W.Window().close() method.

Here's what a close method usually looks like:

    def close(self):
        if self.changed:
            import EasyDialogs
            import Qd
            Qd.InitCursor() # XXX should be done by dialog
            save = EasyDialogs.AskYesNoCancel(
                   'Save window "%s" before closing?' % self.title, 1)
            if save > 0:
                if self.domenu_save():
                    return 1
            elif save < 0:
                return 1
        self.globals = None
        W.Window.close(self)

And here's what the domenu_quit() method of your app object could look like:

    def domenu_quit(self):
        for window in self._windows.values():
            try:
                rv = window.close() # ignore any errors while quitting
            except:
                rv = 0
            if rv and rv > 0:
                return  # don't quit, a save dialog has been cancelled
        self.quitting = 1

Just