为什么这个 python 键盘中断不起作用?(在pycharm中) [英] Why doesn't this python keyboard interrupt work? (in pycharm)

查看:38
本文介绍了为什么这个 python 键盘中断不起作用?(在pycharm中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 pycharm 中调试我的代码时,当按下 Ctrl + C 时,我的 python try/except 循环似乎不会触发键盘中断.我的代码如下所示:

My python try/except loop does not seem to trigger a keyboard interrupt when Ctrl + C is pressed while debugging my code in pycharm. My code look like this:

try:
    while loop:
        print("busy")

except KeyboardInterrupt:
    exit()

我的精简代码似乎存在一些问题,但没有产生相同的错误.完整代码可以查看这里.我还重新精简了代码(上面的代码),它产生了同样的错误.

There seems to be some problems with my slimmed down code working and not producing the same error. The full code can be viewed here. I have also re-slimed down the code (The code above) and it has produced the same error.

推荐答案

从您的屏幕截图看来,您正在 IDE 中运行此代码.IDE 的问题在于它们与正常运行并不完全相同,尤其是在处理键盘字符时.您按 ctrl-c 的方式,您的 IDE 认为您想要复制文本.python 程序永远不会看到这个字符.也许它在运行时会打开一个单独的窗口?然后你会在 ctrl-c 之前选择那个窗口.

From your screen shot it appears that you are running this code in an IDE. The thing about IDEs is that they are not quite the same as running normally, especially when it comes to handling of keyboard characters. The way you press ctrl-c, your IDE thinks you want to copy text. The python program never sees the character. Pehaps it brings up a separate window when running? Then you would select that window before ctrl-c.

这篇关于为什么这个 python 键盘中断不起作用?(在pycharm中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆