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 - 添加多顯示器支持。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM