最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

user interface - How to exit a Python Win32 GUI program on Ctrl-C from invoking console - Stack Overflow

programmeradmin1浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论