pyautogui自動化控制滑鼠和鍵盤操作
- 安裝
- pyautogui滑鼠操作樣例
- pyautogui鍵盤操作樣例
- 按鍵支援
- 提示資訊
-
- alert
- option
- password
- prompt
- 截屏
-
- 整個螢幕截圖并儲存
- 螢幕查找圖檔位置并擷取中間點
- 安全設定
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\')
按鍵支援
按鍵 | 說明 |
---|---|
(或 或 ) | 回車 |
| ESC鍵 |
, | 左右SHIFT鍵 |
, | 左右ALT鍵 |
, | 左右CTRL鍵 |
( ) | TAB鍵 |
, | BACKSPACE 、DELETE鍵 |
, | PAGE UP 和 PAGE DOWN鍵 |
, | HOME 和 END鍵 |
, , , | 箭頭鍵 |
, , …. | F1…….F12鍵 |
, , | 有些鍵盤沒有 |
| PAUSE鍵 |
, , | CAPS LOCK, NUM LOCK, 和 SCROLLLOCK 鍵 |
| INS或INSERT鍵 |
| PRTSC 或 PRINT SCREEN鍵 |
, | Win鍵 |
| Mac 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