当从IDE自动单击时,如何停止python程序?

2024-05-19 09:14:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个控制鼠标的python程序。但是,我希望能够取消运行的程序,而无需将鼠标从程序中夺开。我尝试使用键盘快捷键取消,如下所示:

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print('Interrupted')
        try:
            sys.exit(0)
        except SystemExit:
            os._exit(0)

然而,这也有同样的问题,一旦程序点击离开IDE,我就无法停止程序而不对抗鼠标。这可能吗


Tags: name程序ifmainsysexit鼠标键盘
1条回答
网友
1楼 · 发布于 2024-05-19 09:14:51

PyAutoGUI看起来您可以配置故障保护

As a safety feature, a fail-safe feature is enabled by default. When a PyAutoGUI function is called, if the mouse is in any of the four corners of the primary monitor, they will raise a pyautogui.FailSafeException. There is a one-tenth second delay after calling every PyAutoGUI functions to give the user time to slam the mouse into a corner to trigger the fail safe.

默认情况下,故障保护似乎已启用。验证pyautogui.FAILSAFE == True

在代码中:尝试将KeyboardInterrupt更改为pyautogui.FailSafeException

运行时:在程序调用.1s函数的pyautogui范围内,将鼠标移动到屏幕的一角。我希望这会触发你的sys.exit(0)

相关问题 更多 >

    热门问题