記pyautogui使用方法


記錄學習過程,本人喜歡簡潔不啰嗦:

控制鼠標

 1 pyautogui.moveTo(w - 100, h - 100, duration=0.25)  # 立即移動到指定x, y位置坐標, duration表示移動花費的時間,0表示立即
 2 pyautogui.moveRel(100, 0, duration=0.25)  # 在當前位置移動
 3 pyautogui.position()    # 獲取鼠標當前位置
 4 pyautogui.dragTo()      # 鼠標左鍵移動
 5 pyautogui.dragRel()     # 鼠標右鍵移動
 6 pyautogui.click(100, 150, button='right')  # 默認左鍵  left | right
 7 pyautogui.mouseDown(476, 239)   # 指定按鍵按下
 8 pyautogui.mouseUp(476, 100)     # 指定按鍵松開
 9 pyautogui.doubleClick(100, 230)   #左鍵雙擊
10 pyautogui.rightClick(1000, 200)     # 鼠標右鍵單擊
11 pyautogui.middleClick(100, 200)     # 鼠標中鍵單擊
12 pyautogui.scroll(200)   # 滾輪滾動, 整數向上滾動,負數向下滾動
13 
14 dragRel方法小示例:
15     time.sleep(3)
16     pyautogui.click()
17     dis = 200
18     while dis > 0:
19         pyautogui.dragRel(dis, 0, duration=0.1)
20         dis -= 5
21         pyautogui.dragRel(0, dis, duration=0.1)
22         pyautogui.dragRel(-dis, 0, duration=0.1)
23         dis -= 5
24         pyautogui.dragRel(0, -dis, duration=0.1)

圖片處理

1 img = pyautogui.screenshot()     # 屏幕快照
2 img.getpixel((50, 200))     # 得到圖片坐標 RGB
3 pyautogui.pixelMatchesColor(50, 200, (200, 100, 100))   # 匹配當前屏幕顏色 True | False & 寬,高,RGB顏色
4 pyautogui.locateCenterOnScreen(img)     # 返回圖片中心坐標
5 pyautogui.locateOnScreen(img)   # 完全匹配圖片,未找到返回None
6 pyautogui.locateAllOnScreen(img)    # 返回匹配多處不同的值    [Box(left=0, top=0, width=1920, height=1080)]

控制鍵盤

1 pyautogui.typewrite('hello python', 0.25)     # 文本框輸入內容,需要先確定焦點
2 pyautogui.typewrite(['a', 'b', 'left', 'left', 'X', 'Y'])  # 參考PyKeyboard屬性表
3 pyautogui.keyDown('shift')  # 按下某個鍵
4 pyautogui.press('4')    # 輸入內容
5 pyautogui.keyUp('shift')    # 松開按鍵
6 pyautogui.hotkey('ctrl', 'c')   # 組合鍵

其他方法

1 import pyautogui    # 導入模塊
2 pyautogui.PAUSE = 1  # 每次調用函數后暫停1秒
3 pyautogui.FAILSAFE = True  # 自動防故障
4 x,y = pyautogui.size()   # 獲取x、y坐標

 


免責聲明!

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



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