0,0 X increases --> +---------------------------+ | | Y increases | | | | 1920 x 1080 screen | | | | V | | | | +---------------------------+ 1919, 1079
這是鼠標操做的(x,y)座標圖。能夠看出原點位於屏幕左上角,水平爲x方向,垂直爲y方向,每個點均可以用座標點來表示。看到這裏,我彷彿回到了初三的數學課堂……前端
肯定x,y在屏幕上,用函數onscreen函數
pyautogui.onScreen(0,0)
True
肯定屏幕尺寸,用函數sizi()ui
pyautogui.size()
移動鼠標spa
pyautogui.moveTo(100, 200) #絕對移動 pyautogui.moveRel(100, 200)#相對移動
拖拽鼠標code
pyautogui.dragTo(100, 200, button='left')#絕對移動 pyautogui.dragRel(30, 0, 2, button='right') #相對移動
漸變移動(不重要)blog
pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad) # start slow, end fast pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad) # start fast, end slow pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad) # start and end fast, slow in middle pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce) # bounce at the end pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic) # rubber band at the end
鼠標點擊圖片
pyautogui.click(x=100, y=200) pyautogui.click(clicks=2) pyautogui.doubleClick(buttton='right')
鼠標按下和擡起get
pyautogui.mouseDown()
pyautogui.mouseUp()
拖拽鼠標數學
pyautogui.scroll(10, x=100, y=100) # move mouse cursor to 100, 200, then scroll up 10 "clicks"
輸入字符string
pyautogui.typewrite('Hello world!')
按下鍵,擡起鍵和press(能夠傳列表)
yautogui.keyDown('shift') # hold down the shift key pyautogui.press('left') # press the left arrow key pyautogui.press('left') # press the left arrow key pyautogui.press('left') # press the left arrow key pyautogui.keyUp('shift') # release the shift key
組合熱鍵
pyautogui.hotkey('ctrl', 'shift', 'esc')
pyautogui提供了消息框功能,分別問確認方式、確認和取消方式和輸入框模式
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.'
爲了方便屏幕點擊,pyautogui提供了一個截屏功能,具體以下:
im1 = pyautogui.screenshot() im2 = pyautogui.screenshot('my_screenshot.png') im = pyautogui.screenshot(region=(0,0, 300, 400)) pyautogui.locateOnScreen('calc7key.png') pyautogui.center(button7location) pyautogui.locateCenterOnScreen('calc7key.png') im = pyautogui.screenshot() im.getpixel((100, 200))
經過「locateOnScreen」 定位 按鈕圖片 的座標,而後就能夠直接操做了。
#屏幕截圖 pic = auto.screenshot('my_screenshot.png') #在1920 x 1080屏幕上,screenshot()函數大約須要100毫秒 #圖片必須在pic內可見即窗口在最前端 start_location = auto.locateOnScreen('start.png') #傳入[開始]按鈕狀態1的圖片 if start_location!=None: x,y = auto.center(start_location)#轉化爲 x,y座標 print("座標:",x,y)#按鍵的座標 auto.click(x,y)
做者:一言不合就跑步連接:https://www.jianshu.com/p/ff337d381a64