Skip to main content
 首页 » 编程设计

python之在 PyAutoGUI 故障安全时运行

2024年12月31日85lautakyan007

简而言之,我试图弄清楚如何在 PyAutoGUI 故障保护执行时运行一些代码。我已经多次尝试搜索这个问题,但找不到解决方法。

这就是我想要的:

  1. 将鼠标移到角落并启动故障保护。
  2. 就在程序因故障安全而结束之前,运行一行代码。
  3. 程序完全关闭。

请您参考如下方法:

pyautogui.FailSafeException 当鼠标移动到左上角(x,y of 0, 0)时引发。您可以捕获此异常并从那里运行代码:

导入pyautogui 导入系统

while True: 
    try: 
        pyautogui.moveTo() # Any PyAutoGUI (without side effects) call will do here. 
    except pyautogui.FailSafeException: 
        print('Running code before exiting.') # Your code here. 
        sys.exit()