Thread: OnExit?

    #1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    OnExit?


    Is there anything in Python that works about the same way that Form_Unload() does in Visual Basic 6?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Just for the main Python "command prompt" thing.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
    well, from the title, it seems to be what you want to do is have something executed when the program is shutting down
    If so, you could make a function and then assign all exiting to it (KeyboardInterrupt (Ctrl-C, etc), etc)
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    But what if someone pressed the X in the top right hand corner of the program, does that fall under KeyboardInterrupt also?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    do a try: finally: thing, that will force it to no matter what to do what you want on the exit of the program. So that will handing evyerhting
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    I'd have to try it, but I'd suspect even a try: finally wont work.

    Closing the command environment means there's nothing left for the end of your program to run in...
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    ya your right about the user hiting the exit. I am not sure how you can do it for that. I guess must always make sure your stuff is saved? I really dont' know THat is a good question
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    You want the atexit module.

    The atexit module defines a single function to register cleanup functions. Functions thus registered are automatically executed upon normal interpreter termination.

    Note: the functions registered via this module are not called when the program is killed by a signal, when a Python fatal internal error is detected, or when os._exit() is called.

    register( func[, *args[, **kargs]])

    Register func as a function to be executed at termination. Any optional arguments that are to be passed to func must be passed as arguments to register().
    At normal program termination (for instance, if sys.exit() is called or the main module's execution completes), all functions registered are called in last in, first out order. The assumption is that lower level modules will normally be imported before higher level modules and thus must be cleaned up later.
    Dave - The Developers' Coach
  18. #10
  19. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Thanks Dave. It works perfectly.

IMN logo majestic logo threadwatch logo seochat tools logo