1

I have a tkinter program that when you X out of it a function is called by using:

root.protocol('WM_DELETE_WINDOW', in_close)

Where in_close is a procedure that is called. The issue is that this behavior is not reproduced when you shut down the computer. For the purposes of my program, it is completely necessary for this function to be triggered when the program is closed or you shut the machine down.

I'm on python 3.3.3 just in case. Does anyone know how to reproduce this when it's being shut off? :(

Any ideas are greatly appreciated!! Thanks!!

rodrigocf
  • 1,951
  • 13
  • 39
  • 62

1 Answers1

1

I am not aware of Tkinter having a specific tool to catch system shutdowns like you suggest. You would be better registering a signal handler that can catch the shutdown signal and trigger the in_close function.

(Also worth looking here for some extra details)

import signal

def in_close(signum = None, frame = None):
    # Here you can attempt to gently exit your application.
    pass

# For some common signals that will close your program.
for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]:
    signal.signal(sig, in_close)

As pointed out in the comments, this is not a Windows valid solution. For discussions on handling windows shutdown events see:

Python - Windows Shutdown Events

Community
  • 1
  • 1
ebarr
  • 7,704
  • 1
  • 29
  • 40