Py之PyAutoGUI:python庫之PyAutoGUI的簡介、安裝、使用方法html
目錄python
PyAutoGUI的簡介git
PyAutoGUI的安裝github
PyAutoGUI是一個純Python的GUI自動化工具,其目的是能夠用程序自動控制鼠標和鍵盤操做,利用它能夠實現自動化任務。讓全部GUI都自動化¶ 本教程譯自大神Al Sweigart的PyAutoGUI項目,Python自動化工具,更適合處理GUI任務,網頁任務推薦。PyAutoGUI能夠模擬鼠標的移動、點擊、拖拽,鍵盤按鍵輸入、按住操做,以及鼠標+鍵盤的熱鍵同時按住等操做,能夠說手能動的均可以。函數
The purpose of PyAutoGUI is to provide a cross-platform Python module for GUI automation for human beings. The API is designed to be as simple as possible with sensible defaults.工具
參考文獻
Welcome to PyAutoGUI’s documentation
Doc PyAutoGUIpost
pip install pyautoguiui
哈哈,大功告成!url
#關於庫下函數使用方法概況<br>import pyautogui #關於屏幕分辨率、鼠標座標等 position=pyautogui.position() resolution=pyautogui.size() if_position=pyautogui.onScreen(1900, 2000) print(position,resolution,if_position) #1.一、關於鼠標光標定位 pyautogui.moveTo(screenWidth / 2, screenHeight / 2) pyautogui.moveTo(100,100) pyautogui.moveTo(x=10, y=10, duration=3) #緩動/漸變、Tween/Easing函數:這些效果函數是模仿Al Sweigart的PyTweening模塊,能夠直接使用,不須要額外安裝。 pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad) pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad) pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad) pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce) pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic) pyautogui.moveRel(None, 10) pyautogui.moveRel(xOffset=1000, yOffset=1000, duration=6) #1.二、關於鼠標按下鬆開 pyautogui.mouseDown(button='right'); pyautogui.mouseUp(button='right') pyautogui.mouseDown(button='right') pyautogui.mouseUp(button='right', x=100, y=200) #1.三、關於鼠標點擊 #爲了操做方便,PyAutoGUI提供了doubleClick()、tripleClick()、rightClick()來實現雙擊、三擊、右擊操做 #pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button='left') pyautogui.click(x=100, y=200, duration=2) pyautogui.click(button='right', clicks=2, interval=0.25) pyautogui.dragTo(300, 400, 2, button='left') #1.四、關於鼠標滾動 # pyautogui.scroll(clicks=amount_to_scroll, x=moveToX, y=moveToY) pyautogui.scroll(10) pyautogui.scroll(10, x=100, y=100) #2.一、關於鍵盤按下鬆開 key_name=pyautogui.KEYBOARD_KEYS[:10] pyautogui.keyDown(key_name) pyautogui.keyUp(key_name) #2.2typewrite()普通鍵:鍵盤上能夠按的鍵均可以調用,typewrite()函數只能用於單個字符鍵,不能按SHITF和F1這些功能鍵。 pyautogui.typewrite('Hello world!\n', interval=0.1) pyautogui.typewrite(['a', 'b', 'c', 'left', 'backspace', 'enter', 'f1'], interval=secs_between_keys) #2.3press()功能鍵:press()函數實際上是keyDown()和keyUp()函數的包裝,模擬的按下而後鬆開兩個動做。 pyautogui.press('esc') pyautogui.press('enter') pyautogui.press('f1') pyautogui.press('left') pyautogui.keyUp('shift') #2.4熱鍵組合;('ctrl', 'a')全選、('ctrl', 'c')複製、('ctrl', 'v')粘貼 pyautogui.hotkey('ctrl', 'a') #三、關於消息彈窗函數: pyautogui.alert('這個消息彈窗是文字+OK按鈕') pyautogui.confirm('這個消息彈窗是文字+OK+Cancel按鈕') pyautogui.prompt('這個消息彈窗是讓用戶輸入消息的,單擊OK') #四、關於截屏的函數:屏幕位置使用X和Y軸的笛卡爾座標系。原點(0,0)在左上角,分別向右、向下增大。 若是屏幕像素是 1920×10801920×1080 ,那麼右下角的座標是(1919, 1079)。 pyautogui.screenshot('C:/Users/99386/Desktop/screenshot.png') position_four=pyautogui.locateOnScreen('C:/Users/99386/Desktop/screenshot.png') for i in pyautogui.locateAllOnScreen('C:/Users/99386/Desktop/screenshot.png'): print(i) list1=list(pyautogui.locateAllOnScreen('C:/Users/99386/Desktop/screenshot.png')) position_center=pyautogui.locateCenterOnScreen('C:/Users/99386/Desktop/screenshot.png') print(position_four,list1,position_center)