pyautogui Notes
文章目录
Tutorials
获取位置 position()
屏幕尺寸 size()
给定位置是否在屏幕内 onScreen(x, y)
初始配置
调用延时 pyautogui.PAUSE = 2.5
设置调用延时
- 每一次调用 pyautogui 之后, 会被延时多久
中止方式 pyautogui.FAILSAFE = True
- 鼠标移动到左上角,中止执行
移动操作
绝对移动 moveTo(x, y, duration=num_seconds)
- 即,相对于原点(左上角)的位置
参数
duration=num_seconds
- 整个移动过程用时
相对移动 moveRel(x_offset, y_offset, duration=num_seconds)
- 相对于当前位置的移动
拖拽
绝对拖拽 dragTo(x, y, duration=num_seconds)
相对拖拽 drogRel(x_offset, y_offset, duration=num_seconds)
- 相对当前位置
单击 click()
当前位置,左键单击 click()
绝对移动,再单击 click(x=move_to_x, y=move_to_y, clicks=number_clicks, interval=secs_between_clicks, button='left')
注意
- 这里狗屎关键字参数
参数解说
x, y:
- 绝对位置,相对原点
button:
类型
- 可以是字符串
PRIMARY
- 默认值
根据系统配置决定是左边的按键,还是右边的按键
- 一般是左键
SECONDARY
- 相对于 PRIMARY
- LEFT
- RIGHT
- MIDLE
clicks:
- 点击的总次数
interval:
- 点击之间的间隔
点击快捷函数
左键单击 click(x=move_to_x, y=move_to_y)
左键双击 doubleClick(x=move_to_x, y=move_to_y)
左键击三次 tripleClick(x=move_to_x, y=move_to_y)
右键单击 rightClick(x=move_to_x, y=move_to_y)
单击中键 middleClick(x=move_to_x, y=move_to_y)
滚动 scroll(num_clicks, x=None, y=None, …)
参数
x, y:
- 滚动发生绝对位置
- 测试无效(win10 + Chrome)
None:
- 代表不改变位置,即,还是当前位置
位置参数
num_clicks
- 滚动长度的计量
单位
- 未知
- 感觉像是像素(win10 + Chrome)
方向问题
水平还是竖直?
- 根据环境确定
按下不放(鼠标)mouseDown(), mouseUp()
mouseDown(x=move_to_x, y=move_to_y, button='left')
mouseUp(x=move_to_x, y=move_to_y, button='left')
模拟输入文字 typewrite(your_text, interval=secs_between_keys)
- 模拟敲击键盘打字
参数
your_text
单个字符串
- 'Hello there\n'
字符串列表
- ['a', 'b', 'c', 'left', 'backspace', 'enter', 'f1']
敲击键盘 hotkey()
hotkey('ctrl', 'c')
- Ctrl + c
按下不放键盘 keyDown(key_name), keyUp(key_name)
消息对话框
alert(msg)
返回值
- 永远是:'OK'
confirm(msg)
返回值:
- yes: 'OK'
- no: 'Cancel'
输入内容 promp(msg)
返回值
- 确认:"Your input message"
- 取消:None
截屏 screenshot()
依赖
- Pillow/PIL
获取 图片对象 screenshot()
保存成文件 screenshot('hello.png')
- 同时也返回 一个 Pillow/PIL Image Object
图片操作
位置查找
https://pyautogui.readthedocs.io/en/latest/screenshot.html#the-locate-functions
locateOnScreen('demo.png', grayscale=True, confidence=0.9)
参数
- grayscale=True
加速匹配
confidence=0.9
- 依赖 opencv
返回值
- 找不到,返回None
- 找到,Rectangle tuple:(left, top, width, height)
center(Rectangle) –>(x, y)
locateCenterOnScreen()
locate(needleImage, haystackImage, grayscale=False)
locateAll(needleImage, haystackImage, grayscale=False)
像素获取 pixel(x, y)
| |
像素匹配 pixelMatch(x, y, your_give_pixel_tuple, tolerance=10)
- eg: pixelMatchesColor(100, 200, (140, 125, 134), tolerance=10)
文章作者
上次更新 2022-03-03 (5c64003)