【问题标题】:Python: pyautogui mouse movement in Thread is slow and unreliablePython:pyautogui 鼠标在 Thread 中移动缓慢且不可靠
【发布时间】:2020-01-31 15:07:41
【问题描述】:

我正在尝试自动将鼠标移动到 Python 3 中的某个位置。 为此,我使用了模块pyclicker,特别是模块中的HumanClickerclass。它使用一种算法来计算鼠标移动的“类人”点流。 为了实际沿着计算点移动它,它使用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】:

    我不知道你是否仍然感兴趣,但我在this issue 找到了问题。

    显然 moveTo() 函数定义为def moveTo(x=None, y=None, duration=0.0, tween=linear, logScreenshot=False, _pause=True):

    因此,只需在您的通话中添加参数 _pause=False 即可解决问题,就像它对我所做的那样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-09
      • 2018-01-28
      • 2013-12-03
      • 1970-01-01
      • 2023-03-12
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多