-1

I'm trying to get my tkinter app (python 3.4.2) to be aware of the system shutdown event so it can release the sqlite3 connection and close a log. I found a post from 2009 about using the win32 api module. I can't get the posted sample to work as I expect (I may not understand it), where a message should cause the wndproc function to fire.

2009 reference: Python - Windows Shutdown Events

Any other good references or pointers to how to accomplish this?

Community
  • 1
  • 1
EdLipson
  • 89
  • 2
  • 9
  • Do you really want to trigger this only on system shutdown, or do you want to trigger it whenever your program is forced to exit? – Bryan Oakley Jan 09 '15 at 12:19
  • Mostly system shutdown but any forced exit would be better. I normally restart the system daily per company policy. I assume a restart sends out a shutdown signal. – EdLipson Jan 10 '15 at 16:01

1 Answers1

0

Typically you want to close the connection anytime the app closes, even if it wasn't a normal closure (ie: by picking "Exit" from a menu). The normal way to do that is to set up a handler for the WM_DELETE_WINDOW protocol (something of a dinosaur left over from when tk only worked on X11 systems). I don't know for certain your app will be notified this way when the system shuts down, but it probably does.

For more information see this question on stackoverflow: Intercept Tkinter "Exit" command?

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I tried the WM_DELETE_WINDOW protocol in a small tkinter program. Window close is captured. Shutdown/Restart is not. Since I run the shutdown.exe from a PowerShell script, I guess I can try and send my application a shutdown command somehow, and add that to the script. – EdLipson Jan 11 '15 at 14:57
  • Just tried taskkill, and it responded correctly. That should catch 90-95% of all situations for me. Thanks for the pointer! – EdLipson Jan 11 '15 at 15:06