用pyautogui操作windows


鼠標基本操作

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()

pyautogui.size()

 

移動鼠標

pyautogui.moveTo(100, 200) #絕對移動
pyautogui.moveRel(100, 200)#相對移動

 

拖拽鼠標

pyautogui.dragTo(100, 200, button='left')#絕對移動
pyautogui.dragRel(30, 0, 2, button='right') #相對移動

 

漸變移動(不重要)

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')

 

鼠標按下和抬起

pyautogui.mouseDown()
pyautogui.mouseUp()

 

拖拽鼠標

pyautogui.scroll(10, x=100, y=100)  # move mouse cursor to 100, 200, then scroll up 10 "clicks"

 

鍵盤基本操作

輸入字符

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提供了一個截屏功能,具體如下:

  • 可以直接截屏,當變量使用
  • 也可以截屏,保存在當前文件夾
  • 截屏功能有一個region函數,用來選擇性截屏,用一個tuple來表示left,top,width,height
  • 截屏后可以用locateOnScreen()來測量截屏大小
  • center()函數用了確定截屏中心點x,y
  • 確定某點的顏色
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” 定位 按鈕圖片 的坐標,然后就可以直接操作了。

locateCenterOnScreen(image, grayscale=True),參數grayscale是是否轉灰度,十分有用

#屏幕截圖
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


免責聲明!

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



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