如何防止使用“ctrl”c“终止正在运行的程序在Linux中使用python?

发布于 2024-11-08 01:45:36 字数 263 浏览 1 评论 0原文

我用 python 编写了一段代码,我在其中提出问题,用户应该给出他们的输入。有时,这些问题对于用户来说很难理解(它们不是英语)。所以大多数时候他们想将句子复制粘贴到谷歌翻译中。但是,由于此代码在命令提示符中运行,因此他们必须选择文本并使用“右键单击 --> 复制”将文本复制到谷歌翻译中。有时,会误按“ctrl+c”(每个人都自然地使用此组合进行复制)。这样做会终止代码,他们必须重新开始。我需要知道我可以阻止这种情况发生。换句话说,如果他们按“ctrl+c”,什么也不会发生,我的软件也不会中止。 谢谢

I have written a piece of code in python, in which I am asking questions and users should give their input. Sometimes, these questions are difficult for the user to understand(they are non-english). So most of the time they want to copy paste the sentence into google translate. However, since this code is running in the command prompt,they have to select the text and using "right click --> copy" they can copy the text into google translate. Sometimes, by mistake the press "ctrl+c"(it is natural for everyone to use this combination for copying). Doing this will terminate the code, and they have to start over. I need to know I can prevent this from happening. In other words, if they press "ctrl+c" nothing happens and my software doesn't abort.
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最近可好 2024-11-15 01:45:36
import signal
def SigIntHand(SIG, FRM):
    print("Please Right click-copy. Ctrl-C does not work on the cmd prompt")

signal.signal(signal.SIGINT, SigIntHand)

或者如果你想完全忽略它:

import signal
signal.signal(signal.SIGINT, signal.SIG_IGN)
import signal
def SigIntHand(SIG, FRM):
    print("Please Right click-copy. Ctrl-C does not work on the cmd prompt")

signal.signal(signal.SIGINT, SigIntHand)

or if you want it completely ignored:

import signal
signal.signal(signal.SIGINT, signal.SIG_IGN)
瞎闹 2024-11-15 01:45:36

当您按下 ctrl+c 时,它会向正在运行的进程发送 SIGINT。您可以按照此处所述捕获它。

您可以在此处找到有关不同类型信号的更多信息。

When you hit ctrl+c it sends SIGINT to the running process. You can catch it as described here.

You can find more about the different types of signals here.

笔芯 2024-11-15 01:45:36

如果使用 X,则文本在选择后通常会复制到剪贴板。只需使用鼠标中键或 Shift+insert 粘贴即可。

If using X, the text is normally copied to the clipboard once it's selected. Just paste it using middle mouse button or Shift+insert.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文