pyautogui 文档(一):简介
PyAutoGUI 可实现控制鼠标、键盘、消息框、截图、定位等功能,最近做了个自动化需要这些,故了解并记录下
自动化需要操作win7上的一个app,用PyAutoGUI做的,定位坐标,点击鼠标等,但是对于屏幕的大小分辨率有要求,
不够完善,后利用图片定位一个base坐标,根据base坐标定位其他按钮的位置,才算完成,勉强用,不过学习了行的
PyAUTOGUI,又进一步。
资料来源:https://pyautogui.readthedocs.io/en/latest/general.html
PyAutoGUI适用于Windows / Mac / Linux以及Python 2和3.从PyPI安装。pip install pyautogui
一般功能
>>> pyautogui.position() # current mouse x and y
(968, 56)
>>> pyautogui.size() # current screen resolution width and height
(1920, 1080)
>>> pyautogui.onScreen(x, y) # True if x & y are within the screen.
True
失败保险
在每次PyAutoGUI调用后设置2.5秒的暂停:
>>> import pyautogui
>>> pyautogui.PAUSE = 2.5
当故障安全模式是True,将鼠标移动到左上角将引发pyautogui.FailSafeException可以中止程序的:
>>> import pyautogui
>>> pyautogui.FAILSAFE = True
鼠标功能
XY坐标在屏幕的左上角有0,0原点。X增加向右,Y增加向下。
>>> pyautogui.moveTo(x, y, duration=num_seconds) # move mouse to XY coordinates over num_second seconds
>>> pyautogui.moveRel(xOffset, yOffset, duration=num_seconds) # move mouse relative to its current position
如果duration为0或未指定,则立即移动。注意:在Mac上拖动不能立即。
>>> pyautogui.dragTo(x, y, duration=num_seconds) # drag mouse to XY
>>> pyautogui.dragRel(xOffset, yOffset, duration=num_seconds) # drag mouse relative to its current position
调用click()只需用鼠标当前位置的左键单击鼠标一次,但关键字参数可以改变:
>>> pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button='left')
该button关键字参数可以是'left','middle'或'right'。
所有点击都可以完成click(),但这些功能是为了便于阅读而存在。关键字args是可选的:
>>> pyautogui.rightClick(x=moveToX, y=moveToY)
>>> pyautogui.middleClick(x=moveToX, y=moveToY)
>>> pyautogui.doubleClick(x=moveToX, y=moveToY)
>>> pyautogui.tripleClick(x=moveToX, y=moveToY)
正向滚动将向上滚动,负向滚动将向下滚动:
>>> pyautogui.scroll(amount_to_scroll, x=moveToX, y=moveToY)
可以单独调用单个按钮向下和向上事件:
>>> pyautogui.mouseDown(x=moveToX, y=moveToY, button='left')
>>> pyautogui.mouseUp(x=moveToX, y=moveToY, button='left')
键盘功能
按键可以转到键盘光标处于功能调用时的任何位置。
>>> pyautogui.typewrite('Hello world!\n', interval=secs_between_keys) # useful for entering text, newline is Enter
键名称列表也可以传递:
>>> pyautogui.typewrite(['a', 'b', 'c', 'left', 'backspace', 'enter', 'f1'], interval=secs_between_keys)
密钥名称的完整列表在pyautogui.KEYBOARD_KEYS。
Ctrl-S或Ctrl-Shift-1等键盘热键可以通过将键名列表传递给hotkey():
>>> pyautogui.hotkey('ctrl', 'c') # ctrl-c to copy
>>> pyautogui.hotkey('ctrl', 'v') # ctrl-v to paste
可以单独调用单个按钮向下和向上事件:
>>> pyautogui.keyDown(key_name)
>>> pyautogui.keyUp(key_name)
消息框功能
如果您需要暂停程序直到用户单击“确定”,或者想要向用户显示某些信息,则消息框函数具有与JavaScript类似的名称:
>>> pyautogui.alert('This displays some text with an OK button.')
>>> pyautogui.confirm('This displays text and has an OK and Cancel button.')
'OK'
>>> pyautogui.prompt('This lets the user type in a string and press OK.')
'This is what I typed in.'
如果用户单击“取消”,该prompt()函数将返回None。
截图函数
PyAutoGUI使用Pillow / PIL作为其图像相关数据。
在Linux上,您必须运行才能使用屏幕截图功能。sudo apt-get install scrot
>>> pyautogui.screenshot() # returns a Pillow/PIL Image object
<PIL.Image.Image image mode=RGB size=1920x1080 at 0x24C3EF0>
>>> pyautogui.screenshot('foo.png') # returns a Pillow/PIL Image object, and saves it to a file
<PIL.Image.Image image mode=RGB size=1920x1080 at 0x31AA198>
如果您有一个想要点击的图像文件,可以在屏幕上找到它locateOnScreen()。
>>> pyautogui.locateOnScreen('looksLikeThis.png') # returns (left, top, width, height) of first place it is found
(863, 417, 70, 13)
该locateAllOnScreen()函数将为屏幕上找到的所有位置返回一个生成器:
>>> for i in pyautogui.locateAllOnScreen('looksLikeThis.png')
...
...
(863, 117, 70, 13)
(623, 137, 70, 13)
(853, 577, 70, 13)
(883, 617, 70, 13)
(973, 657, 70, 13)
(933, 877, 70, 13)
>>> list(pyautogui.locateAllOnScreen('looksLikeThis.png'))
[(863, 117, 70, 13), (623, 137, 70, 13), (853, 577, 70, 13), (883, 617, 70, 13), (973, 657, 70, 13), (933, 877, 70, 13)]
该locateCenterOnScreen()函数只返回在屏幕上找到图像的中间的XY坐标:
>>> pyautogui.locateCenterOnScreen('looksLikeThis.png') # returns center x and y
(898, 423)
None如果在屏幕上找不到图像,则会返回这些功能。
注意:定位功能很慢,可能需要一两秒钟。
position() - 返回整数的元组:(x,y)表示鼠标光标的当前位置。
size() - 返回整数的元组:(宽度,高度)表示主监视器的大小。TODO - 添加多显示器支持。
pyautogui 文档(一):简介的更多相关文章
- pyautogui 文档(五):截图及定位功能
截图函数 PyAutoGUI可以截取屏幕截图,将其保存到文件中,并在屏幕中查找图像.如果您有一个小图像,例如需要单击并希望在屏幕上找到它的按钮,这将非常有用.这些功能由PyScreeze模块提供,该模 ...
- 吴裕雄--天生自然PythonDjangoWeb企业开发:Django文档阅读简介
Django是基于MVC模式的框架,虽然也被称为“MTV”的模式,但是大同小异.对我们来说,需要了解的是无论是MVC模式还是MTV模式,甚至是其他的什么模式,都是为了解耦.把一个软件系统划分为一层一层 ...
- pyautogui 文档(四):消息框功能
消息框功能 PyAutoGUI利用PyMsgBox中的消息框函数提供跨平台的纯Python方式来显示JavaScript样式的消息框.提供了四个消息框功能: alert()函数 >>> ...
- pyautogui 文档(三):键盘控制
typewrite()函数 主要的键盘功能是typewrite().此函数将键入字符串中传递的字符.要在按下每个字符键之间添加延迟间隔,请为interval关键字参数传递int float . > ...
- pyautogui 文档(二):鼠标控制
0,0 X increases --> +---------------------------+ | | Y increases | | | | 1920 x 1080 screen | | ...
- Java过滤器详细文档,简介,实例,应用
简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 ...
- ISO/IEC 14496 文档内容简介, MPEG标准
ISO/IEC 14496是MPEG专家组制定的MPEG-4标准于1998年10月公布第1版,1999年1月成为国际标准,1999年12月公布了第2版,2000年初成为国际标准. 全文分为21个部分: ...
- ELK文档--ELK简介
请参考:http://www.cnblogs.com/aresxin/p/8035137.html
- JSP利用freemarker生成基于word模板的word文档
利用freemarker生成基于word模板的word文档 freemarker简介 FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与Web容器 ...
随机推荐
- Java核心-多线程-并发控制器-CyclicBarrier同步屏障
1.基本概念 中文译本同步屏障,同样来自jdk并发工具包中一个并发控制器,它的使用和CountDownLatch有点相似,能够完成某些相同并发场景,但是它们却不相同. 2.抽象模型 主要用来实现多个线 ...
- 用最简单的话告诉你什么是ElasticSearch
介绍 Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elasticsearch 并不仅仅是 L ...
- NPOI helper
using NPOI.HSSF.UserModel; using NPOI.HSSF.Util; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; ...
- Wow64(32位进程)注入DLL到64位进程
转载自: https://blog.poxiao.me/p/wow64-process-inject-dll-into-x64-process/ 向其他进程注入DLL通常的做法是通过调用CreateR ...
- [Notepad++]Notepad++怎么实现双视图/双窗口?
作为windows下非常优秀的开源代码编辑器,Notepad++是工程师必备代码编辑器.相比较之下,老一辈文本编辑器如ultraedit,editplus,就显得繁琐.冗余.疲惫.我使用过Notepa ...
- 在做MVC和WebApi写返回数据时,可以这样定义
public class Messages { /// <summary> /// 返回包含是否成功以及消息字符结果 /// </summary> /// <param ...
- xshell完美开源替代方案(Kitty+MTPuTTY并设置全局字体)
xshell是收费的,过了30天就不能用了.我们应该找一个开源的替代品.说实话windows平台没有什么可选的,就是putty.但是原生的putty不好用,记不住密码,又不支持多标签. Kitty是基 ...
- 移植QT库的问题:QT_INSTALL/include/QtCore/qatomic_arm.h:131: Error: no such instruction: `swpb %al,
解决办法:错误信息说明编译器未识别swpb汇编操作,指令集有问题.把配置命令改成: ./configure -embedded armv7 -prefix /home/thwijeth/Softwar ...
- int main(int argc,char* argv[]) 的含义和用法
1.基本概念 argc,argv 用命令行编译程序时有用. 主函数main中变量(int argc,char *argv[ ])的含义,有些编译器允许将main()的返回类型声明为void,这已不再是 ...
- Linux下安装docker(1)
1.由于centos系统已经自带docker源了,所以可以直接安装: yum install docker 如果是centos6.5版本的,使用yum -y install docker-io 进行安 ...