文章目录
安装
- 安装命令:
pip3 install -i https://pypi.doubanio.com/simple pyautogui - 项目地址
- 中文文档
常用命令
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveTo(x, y)
pyautogui.moveTo(100, 100, duration=2, tween=pyautogui.easeInQuad)
pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad)
pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad)
pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce)
pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic)
pyautogui.moveRel(0, 10)
pyautogui.moveRel(10, 0)
pyautogui.click()
pyautogui.doubleClick()
pyautogui.press('Tab')
pyautogui.keyDown('Alt')
pyautogui.keyUp('Alt')
pyautogui.hotkey('Alt', 'Tab')
pyautogui.write('Hello World!', interval=0.5)
pyautogui.dragTo(100, 200, button='left')
pyautogui.dragTo(300, 400, 2, button='left')
pyautogui.dragRel(0, -60, duration=0.2)
常用弹窗
ret = pyautogui.confirm(text='text', title='title', buttons=['OK', 'Cancel'])
print(ret)
ret = pyautogui.prompt(text='text', title='title', default='default')
print(ret)
图片操作
pix = pyautogui.pixel(220, 200)
print(pix)
ret = pyautogui.pixelMatchesColor(220, 200, (5, 9, 33), tolerance=10)
print(ret)
a = pyautogui.locateOnScreen(r'area.png')
print(a)
x, y = pyautogui.center(a)
print(x, y)
x, y = pyautogui.locateCenterOnScreen(r'area.png')
print(x, y)
a = list(pyautogui.locateAllOnScreen(r'area.png'))
print(a)
for pos in pyautogui.locateAllOnScreen(r'area.png'):
print(pos)
防故障功能
- 由于
pyautogui 会控制我们的鼠标,难免会出现想停止却停止不了的困境,所以可以通过设置 pyautogui.FAILSAFE = True 来启用自动防故障功能,只要将鼠标移到屏幕左上角的坐标(0,0),程序就会抛出failSafeException异常而停止 - 其他参考