小编Adé*_*gun的帖子

Python:Thread中的pyautogui鼠标移动缓慢且不可靠

我正在尝试将一些鼠标自动移动到 Python 3 中的某个位置。为此,我使用 module pyclicker,特别HumanClicker是模块中的类。它使用一种算法来计算鼠标移动的“类似人类”的点流。为了实际沿着它使用的计算点移动它pyautogui.moveTo(). From pyclicker.HumanClicker:

    def move(self, toPoint, duration=2, humanCurve=None):
        fromPoint = pyautogui.position()
        if not humanCurve:
            humanCurve = HumanCurve(fromPoint, toPoint)

        pyautogui.PAUSE = duration / len(humanCurve.points)
        for point in humanCurve.points:
            pyautogui.moveTo(point)
Run Code Online (Sandbox Code Playgroud)

通过移动鼠标和加速/减速,我得到了一些非常好的结果,但是使用 pyautogui 移动鼠标(因此也使用这个 HumanClicker)会锁定程序,直到它完成移动。为了解决这个问题,我在需要时将鼠标移动的处理放入单独的线程中。我的调用代码move()

    def move(self, location, time):
        try:
            hc = HumanClicker()
            hc.move(location, time)
        except TypeError:
            pass
Run Code Online (Sandbox Code Playgroud)

其中位置是浮点数(x, y) tuple,时间是浮点数(当前为 0.2)。对运动进行线程化是可行的,但它会显着减慢运动速度并使其更加不稳定/卡顿(两者都随着运动距离的增加而呈指数级增长)。以任何方式对其进行线程化都会得到相同的结果。如果需要,我可以提供运动记录。

这种互动减慢/卡顿有什么具体原因吗?

是否有一个模块可以替换 pyautogui 以使其在线程中不会出现这些问题?

或者还有其他方法可以解决这个问题吗?

mouse python-multithreading python-3.x pyautogui

6
推荐指数
1
解决办法
7767
查看次数