如何使用 PyAutoGui 加快 while 循环?

How to speed up while loop with PyAutoGui?

我正在为 Discord 制作一个非常简单的垃圾邮件机器人,只是为了恶作剧我的朋友。但是 while True: 命令非常慢。有没有更快的选择?

import PIL
import pyautogui, time
time.sleep(5)
pyautogui.FAILSAFE = True
while True:
    pyautogui.hotkey("command", "v")
    pyautogui.press("enter")
    
if (pyautogui.locateOnScreen("av.png")):
    (pyautogui.click(pyautogui.locateCenterOnScreen("av.png")))

来自documentation

Like the enchanted brooms from the Sorcerer’s Apprentice programmed to keep filling (and then overfilling) the bath with water, a bug in your program could make it go out of control. 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. 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.

You can disable this failsafe by setting pyautogui.FAILSAFE = False. I HIGHLY RECOMMEND YOU DO NOT DISABLE THE FAILSAFE.

The tenth-second delay is set by the pyautogui.PAUSE setting, which is 0.1 by default. You can change this value. There is also a pyautogui.DARWIN_CATCH_UP_TIME setting which adds an additional delay on macOS after keyboard and mouse events, since the operating system appears to need a delay after PyAutoGUI issues these events. It is set to 0.01 by default, adding an additional hundredth-second delay.

因此,如果您想“加速”循环,可以减小 pyautogui.PAUSE 值。但是,请记住,这将阻止您在需要时没有时间激活故障保护。