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

问题描述 投票:0回答:1

我正在尝试在 Python 3 中自动将一些鼠标移动到某个位置。 为此,我使用模块

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)

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

move()
的代码:

    def move(self, location, time):
        try:
            hc = HumanClicker()
            hc.move(location, time)
        except TypeError:
            pass

位置是

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

交互变慢/卡顿有什么具体原因吗?

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

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

python-3.x mouse python-multithreading pyautogui
1个回答
14
投票

我不知道你是否还有兴趣,但我在这个问题上发现了问题。

显然 moveTo() 函数定义为

def moveTo(x=None, y=None, duration=0.0, tween=linear, logScreenshot=False, _pause=True):

因此,只需在通话中添加参数

_pause=False
就可以解决问题,就像我所做的那样。

© www.soinside.com 2019 - 2024. All rights reserved.