小编Luk*_*ang的帖子

如何在 Windows 上捕获 PyCharm 中的停止按钮?

我想创建一个程序,该程序可以通过单击 PyCharm 中的停止按钮来终止脚本。我试过

from sys import exit

def handler(signal_received, frame):
    # Handle any cleanup here
    print('SIGINT or CTRL-C detected. Exiting gracefully')
    exit(0)


if __name__ == '__main__':
    signal(SIGINT, handler)

    print('Running. Press CTRL-C to exit.')
    while True:
        # Do nothing and hog CPU forever until SIGINT received.
        pass
Run Code Online (Sandbox Code Playgroud)

来自https://www.devdungeon.com/content/python-catch-sigint-ctrl-c

我在 Mac 和 Windows 上都尝试过。在 Mac 上,PyCharm 的行为符合预期,当我单击停止按钮时,它会捕获 SIGINT。但在 Windows 上,我做了完全相同的事情,但它只是直接返回给我一个 Process finished with exit code -1. 我可以做些什么来改变以使 Windows 的行为与 Mac 上的行为相似吗?

任何帮助表示赞赏!

python signals keyboardinterrupt pycharm

6
推荐指数
1
解决办法
2562
查看次数

标签 统计

keyboardinterrupt ×1

pycharm ×1

python ×1

signals ×1