python PyAUtoGUI庫實現(xiàn)自動化控制鼠標(biāo)鍵盤
PyAutoGUI 不知道你有沒有用過,它是一款用Python自動化控制鍵盤、鼠標(biāo)的庫。但凡是你不想手動重復(fù)操作的工作都可以用這個庫來解決。
比如,我想半夜時候定時給發(fā)個微信,或者每天自動刷頁面等操作,它能完全模擬手動操作,而你可以安心的刷劇了。
嗯,懶惰是程序員的美德。
安裝pyautogui
pip install pyautogui
鍵盤鼠標(biāo)控制
>>> import pyautogui>>> screenWidth, screenHeight = pyautogui.size() # 返回屏幕分辨率>>> currentMouseX, currentMouseY = pyautogui.position() # 返回鼠標(biāo)的所在位置>>> pyautogui.moveTo(100, 150) #移動鼠標(biāo)到指定位置>>> pyautogui.click() # 單擊>>> pyautogui.click(200, 220) # 單擊指定位置>>> pyautogui.move(None, 10) # 移動鼠標(biāo)10個像素>>> pyautogui.doubleClick() # 雙擊鼠標(biāo)>>> pyautogui.write(’Hello world!’, interval=0.25) # 輸入字符串,每個字符停留時間0.25秒>>> pyautogui.press(’esc’) # 退出鍵>>> pyautogui.keyDown(’shift’) # Shitf鍵盤>>> pyautogui.hotkey(’ctrl’, ’c’) # 組合鍵
用pyautogui自動畫圖
顯示消息盒子
除了可以控制鼠標(biāo)鍵盤外,還可以調(diào)用系統(tǒng)彈窗
>>> import pyautogui>>> pyautogui.alert(’This is an alert box.’)’OK’>>> pyautogui.confirm(’Shall I proceed?’)’Cancel’>>> pyautogui.confirm(’Enter option.’, buttons=[’A’, ’B’, ’C’])’B’>>> pyautogui.prompt(’What is your name?’)’Al’>>> pyautogui.password(’Enter password (text will be hidden)’)’swordfish’
通過窗口消息盒子可以實現(xiàn)一些簡單的人機(jī)交互,比如某些地方需要人工輸入內(nèi)容時,這樣可以接受用戶的指令。
截屏
截圖是使用的Pillow模塊實現(xiàn)的,截圖的用處在于通過圖片識別技術(shù)識別圖片內(nèi)容,然后通過內(nèi)容精準(zhǔn)定位到某個元素的位置,實現(xiàn)精準(zhǔn)點擊。
>>> import pyautogui>>> im1 = pyautogui.screenshot()>>> im1.save(’my_screenshot.png’)>>> im2 = pyautogui.screenshot(’my_screenshot2.png’)
以上就是python PyAUtoGUI庫實現(xiàn)自動化控制鼠標(biāo)鍵盤的詳細(xì)內(nèi)容,更多關(guān)于python PyAUtoGUI庫的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章: