Python 无法使用 PyAutoGui tween 将鼠标作为函数中的随机值移动
Python unable to use PyAutoGui tween to move mouse as random value in a function
我正在尝试使用 pyautogui 中的随机补间函数来随机移动鼠标。我列出了这些函数并使用 random.choice() 随机 select 一个在我的循环中使用。我不知道为什么它不起作用。
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)
知道导致错误的原因吗?
变量 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)
我正在尝试使用 pyautogui 中的随机补间函数来随机移动鼠标。我列出了这些函数并使用 random.choice() 随机 select 一个在我的循环中使用。我不知道为什么它不起作用。
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)
知道导致错误的原因吗?
变量 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)