pyautogui 故障保护的目的是什么?

What is the purpose of the pyautogui failsafe?

所以,当我手动将鼠标移动到屏幕的左上角并且 运行程序,它提出了 pyautogui failsafe.

我确实知道如何禁用它等等,但我想知道为什么它首先存在以及可能的用例

代码:

import pyautogui
pyautogui.click(x=25, y=1048)
time.sleep(2) # I moved the move to the corner of the screen during this time delay
pyautogui.click(x=701, y=430)

错误:

Traceback (most recent call last):
  File "C: Files and Folders\folder\Python Project\Python\CODE\My Projects\Automation.py", line 23, in <module>
    job()
  File "C: Files and Folders\folder\Python Project\Python\CODE\My Projects\Automation.py", line 18, in job
    pyautogui.click(x=1475, y=141)
  File "C:\Users\My_user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pyautogui\__init__.py", line 585, in wrapper
    failSafeCheck()
  File "C:\Users\My_user\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pyautogui\__init__.py", line 1710, in failSafeCheck
    raise FailSafeException(
pyautogui.FailSafeException: PyAutoGUI fail-safe triggered from mouse moving to a corner of the screen. To disable this fail-safe, set pyautogui.FAILSAFE to False. DISABLING FAIL-SAFE IS NOT RECOMMENDED.

根据 pyautogui 文档

It’s hard to use the mouse to close a program if the mouse cursor is moving around on its own.

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.

所以简单地说,FailSafe 就是如果您的程序只是向鼠标发送垃圾邮件或做一些您无法阻止它的事情,您可以通过这种方式关闭它,有关更多信息,请阅读它记录 pyautogui docs

重置

pyautogui.FAILSAFE = False

在你的脚本开头。