How to capture a command prompt window close event in python

后端 未结 1 361
醉梦人生
醉梦人生 2020-12-20 05:20

I want to capture command window close event from Python.
In other words, when the user tries to close the command prompt window, script should detect it and display a m

相关标签:
1条回答
  • 2020-12-20 05:55

    define like this:

    import time
    
    def on_exit(sig, func=None):
        print("exit handler")
        time.sleep(10)  # so you can see the message before program exits
    
    • Windows:

    if you install pywin32 package, you can :

    import win32api
    win32api.SetConsoleCtrlHandler(on_exit, True)
    
    • Un*x:

    Or, using python internal "signal" library, if you are using under *nix system:

    import signal
    signal.signal(signal.SIGTERM, on_exit)
    
    0 讨论(0)
提交回复
热议问题