pyautogui自动化控制鼠标和键盘操作


PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,多平台支持(Windows,OS X,Linux)。

安装

pip3 install pyautogui

pyautogui鼠标操作样例

import pyautogui

# 获取当前屏幕分辨率
screenWidth, screenHeight = pyautogui.size()

# 获取当前鼠标位置
currentMouseX, currentMouseY = pyautogui.position()

# 2秒钟鼠标移动坐标为100,100位置  绝对移动
#pyautogui.moveTo(100, 100,2)
pyautogui.moveTo(x=100, y=100,duration=2, tween=pyautogui.linear)

#鼠标移到屏幕中央。
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)

# 鼠标左击一次
#pyautogui.click()
# x 
# y 
# clicks 点击次数
# interval点击之间的间隔
# button 'left', 'middle', 'right' 对应鼠标 左 中 右或者取值(1, 2, or 3)
# tween 渐变函数
#
pyautogui.click(x=None, y=None, clicks=1, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)

# 鼠标相对移动 ,向下移动
#pyautogui.moveRel(None, 10)
pyautogui.moveRel(xOffset=None, yOffset=10,duration=0.0, tween=pyautogui.linear)


# 鼠标当前位置0间隔双击
#pyautogui.doubleClick()
pyautogui.doubleClick(x=None, y=None, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)

# 鼠标当前位置3击
#pyautogui.tripleClick()
pyautogui.tripleClick(x=None, y=None, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)

#右击
pyautogui.rightClick()

#中击
pyautogui.middleClick()

#  用缓动/渐变函数让鼠标2秒后移动到(500,500)位置
#  use tweening/easing function to move mouse over 2 seconds.
pyautogui.moveTo(x=500, y=500, duration=2, tween=pyautogui.easeInOutQuad)

#鼠标拖拽
pyautogui.dragTo(x=427, y=535, duration=3,button='left')

#鼠标相对拖拽
pyautogui.dragRel(xOffset=100,yOffset=100,duration=,button='left',mouseDownUp=False)

#鼠标移动到x=1796, y=778位置按下
pyautogui.mouseDown(x=1796, y=778, button='left')

#鼠标移动到x=2745, y=778位置松开(与mouseDown组合使用选中)
pyautogui.mouseUp(x=2745, y=778, button='left',duration=5)

#鼠标当前位置滚轮滚动
pyautogui.scroll()
#鼠标水平滚动(Linux)
pyautogui.hscroll()
#鼠标左右滚动(Linux)
pyautogui.vscroll()

pyautogui键盘操作样例

#模拟输入信息
pyautogui.typewrite(message='Hello world!',interval=0.5)
#点击ESC
pyautogui.press('esc')
# 按住shift键
pyautogui.keyDown('shift')
# 放开shift键
pyautogui.keyUp('shift')
# 模拟组合热键
pyautogui.hotkey('ctrl', 'c')

按键支持

按键说明
enter(或return\n)回车
escESC键
shiftleft, shiftright左右SHIFT键
altleft, altright左右ALT键
ctrlleft, ctrlright左右CTRL键
tab (\t)TAB键
backspace, deleteBACKSPACE 、DELETE键
pageup, pagedownPAGE UP 和 PAGE DOWN键
home, endHOME 和 END键
up, down, left,right箭头键
f1, f2, f3….F1…….F12键
volumemute, volumedown,volumeup有些键盘没有
pausePAUSE键
capslock, numlock,scrolllockCAPS LOCK, NUM LOCK, 和 SCROLLLOCK 键
insertINS或INSERT键
printscreenPRTSC 或 PRINT SCREEN键
winleft, winrightWin键
commandMac OS X command键

提示信息

alert

#pyautogui.alert('This is an alert box.','Test')
pyautogui.alert(text='This is an alert box.', title='Test')

在这里插入图片描述

option

#pyautogui.confirm('Shall I proceed?')
pyautogui.confirm('Enter option.', buttons=['A', 'B', 'C'])

在这里插入图片描述

password

a = pyautogui.password('Enter password (text will be hidden)')
print(a)

在这里插入图片描述

prompt

a = pyautogui.prompt('input  message')
print(a)

在这里插入图片描述

截屏

整个屏幕截图并保存

im1 = pyautogui.screenshot()
im1.save('my_screenshot.png')

im2 = pyautogui.screenshot('my_screenshot2.png')

屏幕查找图片位置并获取中间点

#在当前屏幕中查找指定图片(图片需要由系统截图功能截取的图)
coords = pyautogui.locateOnScreen('folder.png')
#获取定位到的图中间点坐标
x,y=pyautogui.center(coords)
#右击该坐标点
pyautogui.rightClick(x,y)

安全设置

import pyautogui

#保护措施,避免失控
pyautogui.FAILSAFE = True
#为所有的PyAutoGUI函数增加延迟。默认延迟时间是0.1秒。
pyautogui.PAUSE = 0.5
相关推荐
<p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; color: #333333;"><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box;"><strong style="margin: 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #ff0000;"><span style="margin: 0px; padding: 0px; font-size: 24px;"><strong style="margin: 0px; padding: 0px; color: #333333;">敬告:</strong></span></span></strong><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; color: #ff0000; font-size: 24px;">该系列的课程在抓紧录制更新中,敬请大家关注。</span></strong></span></span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box;"><strong style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #ff0000;"><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box;"><strong style="margin: 0px; padding: 0px; color: #333333;"><strong style="margin: 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #ff0000; font-size: 24px;"><strong style="margin: 0px; padding: 0px; color: #333333;">敬告:</strong></span></strong></strong><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; color: #00b050;">本课程项目仅供学习参考,请不要直接商用,概不负责任何法律责任</span><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; color: #ff0000;">。</span></strong></span></span></strong></span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; color: #333333;"> </span><span style="margin: 0px; padding: 0px; color: #333333; max-width: 100%; box-sizing: border-box;"><strong style="margin: 0px; padding: 0px;">该系列的课程涉及:FFmpeg,WebRTC,SRS,Nginx,Darwin,Live555,等。包括:音视频、流媒体、直播、Android、视频监控28181、等。</strong></span><br style="margin: 0px; padding: 0px;" /></span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"> </p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; color: #e36c09; font-size: 20px;"><strong style="margin: 0px; padding: 0px;">我将带领大家一起来学习使用FFmpeg开发视频监控项目,<span style="margin: 0px; padding: 0px; color: #00b050;">并动手操练</span>。</strong></span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 20px; color: #000000;"><strong style="margin: 0px; padding: 0px;">具体内容包括:</strong></span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"> </p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 20px; color: #7030a0;">一、视频监控的架构和流程</span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 20px; color: #7030a0;">二、FFmpeg4.3+SDL2+Qt5开发环境的搭建</span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 20px; color: #7030a0;">三、FFmpeg的SDK编程回顾总结并操练</span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 20px; color: #7030a0;">四、SDL2.0的编程回顾总结并操练</span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 20px; color: #7030a0;">五、颜色空间转换RGB和YUV的原理与实战</span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 20px; color: #7030a0;">六、Qt5+FFmpeg本地摄像头采集预览实战</span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 20px; color: #7030a0;">七、代码封装:摄像头h264/5编码并存储</span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 20px; color: #7030a0;">八、Qt5+FFmpeg单路网络摄像头采集预览</span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 20px; color: #7030a0;">九、Qt5+FFmpeg单路网络摄像头</span><span style="margin: 0px; padding: 0px; font-size: 20px; color: #ff0000;">采集预览录制会看</span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 20px; color: #7030a0;">十、onvif与GB/T-28181的简介</span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"> </p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><img src="https://img-bss.csdnimg.cn/202101130753276832.jpg" alt="" /></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="color: #313d54; font-family: Helvetica Neue, Helvetica, Hiragino Sans GB, Arial, sans-serif;"> </span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><strong><span style="color: #313d54; font-family: Helvetica Neue, Helvetica, Hiragino Sans GB, Arial, sans-serif;">音视频与流媒体是一门很复杂的技术,涉及的概念、原理、理论非常多,很多初学者不学 基础理论,而是直接做项目,往往会看到c/c++的代码时一头雾水,不知道代码到底是什么意思,这是为什么呢?</span></strong></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="color: #313d54; font-family: Helvetica Neue, Helvetica, Hiragino Sans GB, Arial, sans-serif;"> </span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="color: #313d54; font-family: Helvetica Neue, Helvetica, Hiragino Sans GB, Arial, sans-serif;"> </span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="color: #313d54; font-family: Helvetica Neue, Helvetica, Hiragino Sans GB, Arial, sans-serif;"> </span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="color: #313d54; font-family: Helvetica Neue, Helvetica, Hiragino Sans GB, Arial, sans-serif;">因为没有学习音视频和流媒体的基础理论,就比如学习英语,不学习基本单词,而是天天听英语新闻,总也听不懂。</span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="color: #313d54; font-family: Helvetica Neue, Helvetica, Hiragino Sans GB, Arial, sans-serif;"> </span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="color: #313d54; font-family: Helvetica Neue, Helvetica, Hiragino Sans GB, Arial, sans-serif;">所以呢,一定要认真学习基础理论,然后再学习播放器、转码器、非编、流媒体直播、视频监控、等等。</span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="color: #313d54; font-family: Helvetica Neue, Helvetica, Hiragino Sans GB, Arial, sans-serif;"> </span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="color: #313d54; font-family: Helvetica Neue, Helvetica, Hiragino Sans GB, Arial, sans-serif;"> </span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="color: #313d54; font-family: Helvetica Neue, Helvetica, Hiragino Sans GB, Arial, sans-serif;"> </span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><strong><span style="color: #313d54; font-family: Helvetica Neue, Helvetica, Hiragino Sans GB, Arial, sans-serif;">梅老师从事音视频与流媒体行业18年;曾在永新视博、中科大洋、百度、美国Harris广播事业部等公司就职,经验丰富;曾亲手主导广电直播全套项目,精通h.264/h.265/aac,曾亲自参与百度app上的网页播放器等实战产品。</span></strong></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"> </p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="color: #313d54; font-family: Helvetica Neue, Helvetica, Hiragino Sans GB, Arial, sans-serif;"> </span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="color: #313d54; font-family: Helvetica Neue, Helvetica, Hiragino Sans GB, Arial, sans-serif;">目前全身心自主创业,主要聚焦音视频+流媒体行业,精通音视频加密、流媒体在线转码快编等热门产品。</span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"><span style="color: #313d54; font-family: Helvetica Neue, Helvetica, Hiragino Sans GB, Arial, sans-serif;"> </span></p> <p style="font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif; margin: 0px; padding: 0px; color: #313d54; font-size: 16px; background-color: #ffffff;"> </p>
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页