I have a simple Python Win32 program that works well when run under pythonw
from a command prompt. When I run it under python
, it keeps running in the foreground of the command prompt and can e.g. print to STDOUT, which is very useful for debugging. However, it ignores Ctrl-C. I would like to have it exit when Ctrl-C is sent to the console it is running from.
Relevant code is:
update_gui_thread = threading.Thread(target=gui_clock)
update_gui_thread.start()
win32gui.PumpMessages()
def signal_handler(sig, frame):
print('Ctrl-C pressed, exiting.')
win32gui.PostQuitMessage(0)
sys.exit(0)
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal_handler)
main()
When I hit Ctrl-C, it prints to Console Ctrl-C pressed, exiting. Python WNDPROC handler failed
. The GUI freezes but stays on the screen. Only manually terminating the process via Task Manager make the GUI go away.
I think that most likely the update_gui_thread
is exiting but not the main window.
How can I make it exit completely on Ctrl-C?