ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python pyauto模块详解

2022-04-26 04:31:05  阅读:158  来源: 互联网

标签:pyautogui 鼠标 pyauto python moveToY moveToX 详解 result left


# 获取当前鼠标位置print(pyautogui.position())
在每次调用PyAutoGUI的函数后设置2.5秒的暂停:

# 暂停2.5s
pyautogui.PAUSE = 2.5

此外,为了防止程序出问题,当鼠标移动到屏幕左上角,会引发pyautogui.FailSafeException错误进而中止程序。
关闭命令如下(不建议关闭):

pyautogui.FAILSAFE = False
鼠标移动

# 用num_seconds(秒)将鼠标移动到(x,y)位置

x = 200
y = 100

num_seconds = 1

pyautogui.moveTo(x, y, duration=num_seconds)

# 用num_seconds(秒)将鼠标从当前位置向右移动xOffset,向下移动yOffset

# 如果duration为0或未指定,则立即移动。

xOffset = 30

yOffset = -50

num_seconds = 0.5

pyautogui.moveRel(xOffset, yOffset, duration=num_seconds)


鼠标拖动
# 用num_seconds(秒)将鼠标推动到(x,y)位置
# 鼠标拖动是指按下鼠标左键移动鼠标。
x = 200
y = 100
num_seconds= 1
pyautogui.dragTo(x, y, duration=num_seconds)

# 用num_seconds(秒)将鼠标从当前位置向右拖动xOffset,向下推动yOffset
xOffset = 30
yOffset = -50
num_seconds = 0.5
pyautogui.dragRel(xOffset, yOffset, duration=num_seconds) 

鼠标单击
# 将鼠标移动到(moveToX,moveToY)位置,点击鼠标num_of_clicks次,每次点击间隔secs_between_clicks秒
# button表示单击方式,'left'左键单击,'middle'中键单击,'right'右键单击
moveToX = 500
moveToY = 600
num_of_clicks = 1
secs_between_clicks = 1
pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button='left')
所有的鼠标点击都可以用click()完成,但也存在一些函数是为了方便阅读,如下所示。

moveToX = 10
moveToY = 20
# 右键单击
pyautogui.rightClick(x=moveToX + 50, y=moveToY)
# 中键单击
pyautogui.middleClick(x=moveToX + 50, y=moveToY)
# 左键双击
pyautogui.doubleClick(x=moveToX + 50, y=moveToY)
# 左键三击
pyautogui.tripleClick(x=moveToX + 50, y=moveToY)
4.4 鼠标滚动
moveToX = 100
moveToY = 200
# 鼠标在当前位置向下滑动100格
# pyautogui.scroll(clicks=-100)
# 鼠标移动到(moveToX,moveToY)位置,然后向上滚动150格
pyautogui.scroll(clicks=150, x=moveToX, y=moveToY)
4.5 鼠标按下
# 鼠标移动到(moveToX,moveToY)位置,鼠标左键按下
pyautogui.mouseDown(x=moveToX, y=moveToY, button='left')
# 鼠标移动到(moveToX,moveToY)位置,鼠标右键松开(按下右键的情况下)
pyautogui.mouseUp(x=moveToX, y=moveToY, button='right')
# 鼠标在当前位置,按下中键
pyautogui.mouseDown(button='middle')
4.6 缓动/渐变(Tween / Easing)函数
缓动/渐变函数的作用是让光标的移动更炫。如果你不需要用到的话,你可以忽略这些。PyAutoGUI有30种缓动/渐变函数,可以通过以下函数查看

print(pyautogui.ease*?)
常用缓动/渐变函数使用示例如下:

moveToX = 100
moveToY = 100
# #开始慢,结束快
pyautogui.moveTo(moveToX + 5 , moveToY+ 45, 2, pyautogui.easeInQuad)
# 开始快,结束慢
pyautogui.moveTo(moveToX + 15, moveToY+ 35, 2, pyautogui.easeOutQuad)
# 快速开始和结束,中间缓慢
pyautogui.moveTo(moveToX + 25, moveToY+ 25, 2, pyautogui.easeInOutQuad)
# 最后反弹
pyautogui.moveTo(moveToX + 35, moveToY+ 15, 2, pyautogui.easeInBounce)
# 反复横跳
pyautogui.moveTo(moveToX + 45, moveToY+ 5, 2, pyautogui.easeInElastic)
5 键盘函数
5.1 文字输入
键盘控制文字输入的主要函数就是typewrite()/write()。这个函数可以实现字符输入,可以用interval参数设置两次输入间时间间隔。

# 在当前位置输入文字text,每个字符输入间隔secs_between_keys秒
# \n表示换行
text = 'Hello world!\n'
secs_between_keys = 0.1
pyautogui.typewrite(message=text, interval=secs_between_keys)
# 在当前位置按下键盘各种键
pyautogui.typewrite(['\t', 'a', 'b', 'c', 'left', 'backspace', 'enter', 'f1','\n'], interval=secs_between_keys)
# 查看所有支持按键
print(pyautogui.KEYBOARD_KEYS)
5.2 快捷键
通过keyDown/keyUp按下或者松开键盘,通过hotkey执行快捷键操作。

# ctrl+c 复制文字
pyautogui.hotkey('ctrl', 'c')
# ctrl+v 粘贴文字
pyautogui.hotkey('ctrl', 'v')

# 按下ctrl键
pyautogui.keyDown('ctrl')
# 按下v键,相当文字粘贴
pyautogui.keyDown('v')
# 松开ctrl键盘
pyautogui.keyUp('ctrl')
当然可以使用press()函数设置按下某个键再释放某个键,如下所示。

# 按下shift键
pyautogui.keyDown('shift')
pyautogui.press('left')
pyautogui.press('left')
pyautogui.press('left')
# 松开shift键
pyautogui.keyUp('shift')
同时也可以和typewrite()函数一样,用数组把一组键传入press(),或者设置press按压次数。

# 按下三个left键
pyautogui.press(['left', 'left', 'left'])
# 按left键五次
pyautogui.press('left', presses=5)
5.3 hold()上下文管理器
hold()函数可以用作上下文管理器,并从pyautogui.KEYBOARD_KEYS传递一个字符串,并且该键将在上下文块的持续时间内保持。示例如下:

# 按住shift
with pyautogui.hold('shift'):
# 连续按left,然后松开shift
pyautogui.press(['left', 'left', 'left'])

# 上面代码功能和下面代码实现功能相同
# 按下shift键
pyautogui.keyDown('shift')
pyautogui.press('left')
pyautogui.press('left')
pyautogui.press('left')
# 松开shift键
pyautogui.keyUp('shift')
6 消息框函数
如果你需要暂停程序直到用户点击确定,或者想向用户显示一些信息,可以使用消息框函数。这里消息框函数的使用方式和javascript一样。

# 警告窗口
alert_result = pyautogui.alert('点击确定返回字符串OK')
# 确认窗口
confirm_result = pyautogui.confirm('点击确定返回字符串OK,点击取消返回字符串Cancel')
# 点击ok保存输入的文字,点击Cancel返回None
prompt_result = pyautogui.prompt('输入文字')
# 点击ok保存输入的密码,点击Cancel返回None
# default默认文字,mask用什么符号代替输入的密码
password_result = pyautogui.password(text='', title='', default='', mask='*')
7 截图函数
PyAutoGUI使用Pillow/PIL库实现图像的处理。在Linux上,您必须运行以下命令安装scrot库才能使用屏幕截图功能。

sudo apt-get install scrot
7.1 截屏
# 截屏返回result对象
result = pyautogui.screenshot()
# result是PIL中的Image对象
print(type(result))
# 保存图像
result.save('result1.jpg')
# 展示图片
#result.show()

# imageFilename参数设置文件保存为止,在截屏前保存图片到本地foo.png文件
# region设置截图区域[x,y,w,h],以(x,y)为左上角顶点,截宽w,高h的区域
result = pyautogui.screenshot(imageFilename='result2.jpg',region=[10,20,100,50])

7.2 图像定位
PyAutoGUI提供了多个定位函数。都是从左上角原点开始向右向下搜索截图位置。具体如下:

locateOnScreen(image, grayscale=False):在屏幕中,返回和image图片最类似区域的坐标(left, top, width, height),如果没找到返回None。grayscale设置是否灰度查找。
locateCenterOnScreen(image, grayscale=False):在屏幕中,返回和image图片最类似区域的中心坐标(x, y),如果没找到返回None。
locateAllOnScreen(image, grayscale=False):在屏幕中,返回和image图片所有类似区域的坐标(left, top, width, height)的生成器
locate(needleImage, haystackImage, grayscale=False):在haystackImage中,返回和image图片最类似区域的坐标(left, top, width, height)。
locateAll(needleImage, haystackImage, grayscale=False):在haystackImage中,返回和image图片所有类似区域的坐标(left, top, width, height)的生成器。
官方说在1920x1080屏幕上,screenshot()函数大约需要100毫秒。但实测图像定位需要花费3秒左右,而且常常找不到图片相似区域。可选的confidence关键字参数指定函数在屏幕上定位图像的准确性。如果由于像素差异可忽略不计,函数无法定位图像,调低confidence将提高查找命中结果。但是需要安装OpenCV才能使confidence关键字工作。

图像定位函数基础使用如下:

# 在屏幕返回和result1.jpg图片类似的区域坐标,返回值(左上角x坐标,左上角y坐标,宽度,高度)
# 如果没找到返回None
result = pyautogui.locateOnScreen('result1.jpg')
# 在屏幕返回和result1.jpg图片类似的区域中间位置的XY坐标,confidence返回区域最低置信度
result = pyautogui.locateCenterOnScreen('result1.jpg', confidence=0.9)
# 为查找图片找到的所有位置返回一个生成器
results = pyautogui.locateAllOnScreen('result1.jpg', confidence=0.6)
print(results)
# 打印各组的(左上角x坐标,左上角y坐标,宽度,高度)
for i in results:
print(i)
# 将结果保存为list
list_result = list(pyautogui.locateAllOnScreen('result1.jpg', confidence=0.6)

# 在haystackImage中,返回和image图片最类似区域的坐标
result = pyautogui.locate(needleImage='result1.jpg', haystackImage='result.jpg', confidence=0.5)
# 在haystackImage中,返回和image图片所有类似区域的坐标(left, top, width, height)
result = pyautogui.locateAll(needleImage='result1.jpg', haystackImage='result.jpg', confidence=0.5)
这些“定位”功能相当昂贵;他们可能需要整整几秒钟的时间才能运行。加速它们的最好方法是传递一个region参数(一个(左、上、宽、高)的4整数元组)来只搜索屏幕的较小区域而不是全屏。但是这个region区域必须比待搜索截图区域大,否则会引发错误。代码如下:

result = pyautogui.locateOnScreen('result1.jpg', region=(0,0, 300, 400))
result = pyautogui.locate(needleImage='result1.jpg', haystackImage='result.jpg', confidence=0.5, region=(0,0, 300, 400))
您可以传递grayscale=True给定位函数以提供轻微的加速(大约30%左右)。这会降低图像和屏幕截图的颜色饱和度,加快定位速度,但可能会导致误报匹配。

result_location = pyautogui.locateOnScreen('result.jpg', grayscale=True,confidence=0.6)
此外要获取截屏某个位置的RGB像素值,可以用PIL中Image对象的getpixel()方法,也可以用PyAutoGUI的pixel()函数。

im = pyautogui.screenshot()
print(im.getpixel((100, 200)))
print(pyautogui.pixel(100, 200))
如果您只需要验证单个像素是否与给定像素匹配,请调用该pixelMatchesColor()函数,并将其表示的颜色的X坐标、Y坐标和RGB元组传递给它:

# 颜色匹配
pyautogui.pixelMatchesColor(100, 200, (255, 255, 255))
# tolerance参数可以指定红、绿、蓝3种颜色误差范围
pyautogui.pixelMatchesColor(100, 200, (248, 250, 245), tolerance=10)

搜索

复制

标签:pyautogui,鼠标,pyauto,python,moveToY,moveToX,详解,result,left
来源: https://www.cnblogs.com/xkdn/p/16193147.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有