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!
', 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 - 添加多显示器支持。

原文地址:https://www.cnblogs.com/gexbooks/p/10789317.html