Python:线程中的pyautogui鼠标移动缓慢且不可靠 [英] Python: pyautogui mouse movement in Thread is slow and unreliable
问题描述
我试图在 Python 3 中自动将鼠标移动到某个位置.为此,我使用了一个模块 pyclicker
,特别是模块中的 HumanClicker
类.它使用一种算法来计算鼠标移动的类人"点流.为了沿着计算点实际移动它,它使用 pyautogui.moveTo().来自 pyclicker.HumanClicker:
I am attempting to automate some mouse movement to a certain position in Python 3.
For this, I am using a module pyclicker
, specifically the HumanClicker
class from the module. It uses an algorithm to calculate a 'human-like' flow of points to the mouse movement.
For actually moving it along the calculated points it uses 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()
的代码:
I get some very nice results with moving the mouse and speeding up/slowing down, but moving the mouse with pyautogui (and because of that also with this HumanClicker) locks up the program until it is done moving. To fix this, I put the handling of the mouse movement into a separate thread whenever needed. My code for calling the move()
:
def move(self, location, time):
try:
hc = HumanClicker()
hc.move(location, time)
except TypeError:
pass
其中 location 是一个 (x, y) tuple
和 time 一个浮点数(当前为 0.2).线程化运动是有效的,但它会显着减慢运动速度并使其更加不稳定/断断续续(两者都随着它必须行进的距离呈指数级增长).以任何方式对其进行线程化都会产生相同的结果.如果需要,我可以提供运动记录.
Where location is an (x, y) tuple
and time a float (currently 0.2). Threading the movement works, but it significantly slows down the movement and makes it much more erratic/stuttery (both scale exponentially with the distance it has to travel). Threading it in any way gives the same results. I can provide a recording of the movement if needed.
这种交互变慢/口吃有什么具体原因吗?
Is there any specific reason for this slowing down/stuttering interaction?
是否有一个模块可以替换 pyautogui 以使其在线程处理中不会出现这些问题?
Is there a module I could replace pyautogui with to make it not have these problems in threading?
或者有没有其他方法可以解决这个问题?
Or is there any other way to fix this issue?
推荐答案
我不知道你是否仍然感兴趣,但我在 这个问题.
I don't know if you are still interested but I've found the problem on this issue.
显然 moveTo() 函数定义为 def moveTo(x=None, y=None, duration=0.0, tween=linear, logScreenshot=False, _pause=True):
Apparently the moveTo() function is defined as def moveTo(x=None, y=None, duration=0.0, tween=linear, logScreenshot=False, _pause=True):
因此,只需在您的通话中添加参数 _pause=False
就可以像对我一样解决问题.
So simply adding the argument _pause=False
on your call should fix the issue as it did for me.
这篇关于Python:线程中的pyautogui鼠标移动缓慢且不可靠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!