Python无法使用PyAutoGui补间将鼠标作为函数中的随机值移动
Posted
技术标签:
【中文标题】Python无法使用PyAutoGui补间将鼠标作为函数中的随机值移动【英文标题】:Python unable to use PyAutoGui tween to move mouse as random value in a function 【发布时间】:2022-01-19 20:23:38 【问题描述】:我正在尝试使用 pyautogui 中的随机补间函数来随机移动鼠标。我列出了这些函数,并使用 random.choice() 随机选择一个在我的循环中使用。我不知道为什么它不起作用。
import pyautogui as p
import random
game_window = 'game.png'
games = p.locateAllOnScreen(game_window, confidence=0.8,
region=(0, 1400, 3000, 40))
mouse_random_moves = ('p.easeOutCubic', 'p.easeOutQuint', 'p.easeInQuart',
'p.easeInOutBounce', 'p.easeInOutBack', 'p.easeInCubic',)
for game in games:
move = random.choice(mouse_random_moves)
left, top, width, height = game
click_window = p.center(game)
x, y = click_window
p.moveTo(x, y, duration=1, tween=move)
我收到一个错误:
Traceback (most recent call last):
File "C:\Users\rysik\Documents\python_work\test\test.py", line 15, in <module>
p.moveTo(x, y, duration=1, tween=move)
File "C:\Users\rysik\Documents\python_work\test\venv\lib\site-packages\pyautogui\__init__.py", line 598, in wrapper
returnVal = wrappedFunction(*args, **kwargs)
File "C:\Users\rysik\Documents\python_work\test\venv\lib\site-packages\pyautogui\__init__.py", line 1283, in moveTo
_mouseMoveDrag("move", x, y, 0, 0, duration, tween)
File "C:\Users\rysik\Documents\python_work\test\venv\lib\site-packages\pyautogui\__init__.py", line 1483, in _mouseMoveDrag
steps = [getPointOnLine(startx, starty, x, y, tween(n / num_steps)) for n in range(num_steps)]
File "C:\Users\rysik\Documents\python_work\test\venv\lib\site-packages\pyautogui\__init__.py", line 1483, in <listcomp>
steps = [getPointOnLine(startx, starty, x, y, tween(n / num_steps)) for n in range(num_steps)]
TypeError: 'str' object is not callable
但是,如果我只是将补间函数分配给变量,那么它就可以工作。这将移动鼠标:
mouse = p.easeOutBack
p.moveTo(x, y, duration=1, tween=mouse)
知道是什么导致了错误吗?
【问题讨论】:
【参考方案1】:变量 mouse_random_moves 具有字符串值。在您的 mouse 示例中,它不是一个字符串值,而是一个函数。删除 mouse_random_moves 值中的单引号。
你能不能试试下面的代码:
import pyautogui as p
import random
game_window = 'game.png'
games = p.locateAllOnScreen(game_window, confidence=0.8,
region=(0, 1400, 3000, 40))
mouse_random_moves = (p.easeOutCubi, p.easeOutQuint, p.easeInQuart,
p.easeInOutBounce, p.easeInOutBack, p.easeInCubic)
for game in games:
move = random.choice(mouse_random_moves)
left, top, width, height = game
click_window = p.center(game)
x, y = click_window
p.moveTo(x, y, duration=1, tween=move)
【讨论】:
以上是关于Python无法使用PyAutoGui补间将鼠标作为函数中的随机值移动的主要内容,如果未能解决你的问题,请参考以下文章
python使用笔记:pyautogui自动化控制鼠标和键盘
自动化工具:PyAutoGUI的鼠标与键盘控制,解放双手的利器