2025年3月4日 星期二

PyAutoGUI 常用指令

 

 PyAutoGUI 常用指令

功能

指令

說明

範例

滑鼠控制

pyautogui.moveTo(x, y)

將滑鼠游標移動到指定座標 (x, y)。座標原點 (0, 0) 為螢幕左上角。X 軸向右為正,Y 軸向下為正。

python import pyautogui # 移動到絕對座標 (100, 200) pyautogui.moveTo(100, 200)

pyautogui.moveRel(xOffset, yOffset)

相對於目前位置移動滑鼠。xOffset 為水平方向偏移量,yOffset 為垂直方向偏移量。

python import pyautogui # 相對目前位置向右移動 50 像素,向下移動 100 像素 pyautogui.moveRel(50, 100)

pyautogui.click(x, y, button='left')

在指定座標 (x, y) 點擊滑鼠。button 參數可指定按鍵,預設為 'left' (左鍵)。其他選項包括 'right' (右鍵)'middle' (中鍵)

python import pyautogui # 在座標 (100, 200) 點擊滑鼠左鍵 pyautogui.click(100, 200)

pyautogui.doubleClick(x, y, button='left')

在指定座標 (x, y) 雙擊滑鼠。

python import pyautogui # 在座標 (300, 400) 雙擊滑鼠左鍵 pyautogui.doubleClick(300, 400)

pyautogui.rightClick(x, y)

在指定座標 (x, y) 點擊滑鼠右鍵。

python import pyautogui # 在座標 (500, 600) 點擊滑鼠右鍵 pyautogui.rightClick(500, 600)

pyautogui.middleClick(x, y)

在指定座標 (x, y) 點擊滑鼠中鍵。

未提供範例

pyautogui.dragTo(x, y, button='left')

將滑鼠游標拖曳到指定座標 (x, y)

python import pyautogui # 從目前位置拖曳到座標 (500, 300) pyautogui.dragTo(500, 300)

pyautogui.dragRel(xOffset, yOffset, button='left')

相對於目前位置拖曳滑鼠。

python import pyautogui # 相對目前位置向右拖曳 200 像素,向下拖曳 100 像素 pyautogui.dragRel(200, 100)

pyautogui.scroll(clicks)

滾動滑鼠滾輪。clicks 為滾動的格數,正數為向上滾動,負數為向下滾動。

python import pyautogui # 向上滾動 10 pyautogui.scroll(10) # 向下滾動 5 pyautogui.scroll(-5)

pyautogui.position()

回傳滑鼠目前位置的座標。

未提供範例

鍵盤控制

pyautogui.typewrite(message)

輸入字串 message。每個字元之間會自動停頓一小段時間,避免輸入過快。可以設定 interval 參數調整停頓時間 (單位為秒)

python import pyautogui # 輸入字串 "Hello, PyAutoGUI!" pyautogui.typewrite("Hello, PyAutoGUI!") # 0.2 秒的間隔輸入字串 "This is a test." pyautogui.typewrite("This is a test.", interval=0.2)

pyautogui.press(key)

模擬按下 key 按鍵。key 可以是單個按鍵 (例如:'a''enter''esc'),或是特殊按鍵的名稱 (例如:'up''down''left''right''f1''f2' )

python import pyautogui # 按下 Enter pyautogui.press('enter')

pyautogui.keyDown(key)

模擬按下 key 按鍵,但不放開。

python import pyautogui # 按下 Ctrl pyautogui.keyDown('ctrl') # 按下 c pyautogui.press('c')

pyautogui.keyUp(key)

模擬放開 key 按鍵。

python import pyautogui # 放開 Ctrl pyautogui.keyUp('ctrl')

pyautogui.hotkey(key1, key2, ...)

模擬同時按下多個按鍵。例如:pyautogui.hotkey('ctrl', 'c') 相當於按下 Ctrl+C 組合鍵。

python import pyautogui # 模擬按下 Ctrl+Shift+Esc 組合鍵,開啟工作管理員 pyautogui.hotkey('ctrl', 'shift', 'esc') # 模擬按下 Ctrl+A 組合鍵,全選 pyautogui.hotkey('ctrl', 'a')

螢幕控制

pyautogui.screenshot()

截取整個螢幕的畫面,並回傳一個 Pillow Image 物件。

python import pyautogui # 截取整個螢幕畫面 screenshot = pyautogui.screenshot() screenshot.save("screenshot.png")

pyautogui.screenshot(region=(x1, y1, x2, y2))

截取指定區域的畫面。region 參數為一個 tuple,包含左上角座標 (x1, y1) 和右下角座標 (x2, y2)

python import pyautogui # 截取指定區域畫面 screenshot = pyautogui.screenshot(region=(100, 100, 300, 300)) screenshot.save("region_screenshot.png")

image.save(filename)

將截圖儲存到指定檔案。

見上方範例

pyautogui.locateOnScreen(image, region=None)

在螢幕上尋找指定圖片,並回傳一個 Generator 物件,包含所有找到的圖片的位置。image 參數為要尋找的圖片檔案名稱。region 參數為指定搜尋區域,預設為整個螢幕。

python import pyautogui # 在螢幕上尋找 "button.png" 圖片 locations = pyautogui.locateOnScreen("button.png") for location in locations: print(f"找到圖片在 {location}")

pyautogui.locateCenterOnScreen(image, region=None)

在螢幕上尋找指定圖片,並回傳圖片中心點的座標。

python import pyautogui # 在螢幕上尋找 "icon.png" 圖片,並取得中心點座標 center = pyautogui.locateCenterOnScreen("icon.png") if center: print(f"圖片中心點座標為 {center}")

視窗控制

pyautogui.getActiveWindow()

取得目前 active 的視窗。

未提供範例

pyautogui.getWindowsWithTitle(title)

取得所有標題包含指定字串的視窗。

未提供範例

window.moveTo(x, y)

移動視窗到指定座標。

未提供範例

window.resizeTo(width, height)

調整視窗大小。

未提供範例

window.maximize()

最大化視窗。

未提供範例

window.minimize()

最小化視窗。

未提供範例

window.close()

關閉視窗。

未提供範例

其他

pyautogui.size()

回傳螢幕寬度和高度。

未提供範例

pyautogui.alert(text, title='訊息')

顯示警告訊息框。

python import pyautogui # 顯示警告訊息框 pyautogui.alert("Hello, PyAutoGUI!")

pyautogui.confirm(text, title='確認')

顯示確認訊息框,回傳 True False

python import pyautogui # 顯示確認訊息框 result = pyautogui.confirm("確定要執行此操作嗎?") if result: print("使用者點擊了確認") else: print("使用者點擊了取消")

pyautogui.prompt(text, title='輸入')

顯示輸入訊息框,回傳使用者輸入的文字。

python import pyautogui # 顯示輸入訊息框 text = pyautogui.prompt("請輸入文字:") print(f"使用者輸入了:{text}")

time.sleep(seconds)

讓程式暫停指定秒數。

未提供範例

pyautogui.waitFor(seconds, callback=None)

等待指定秒數,或直到 callback 函式回傳 True

未提供範例

pyautogui.FAILSAFE = True

當滑鼠游標移動到螢幕左上角時,觸發 pyautogui.FailSafeException 例外,可以強制停止程式。

未提供範例

pyautogui.PAUSE = 2.5

設定 PyAutoGUI 函式之間的暫停時間,避免操作過快導致錯誤。

未提供範例

請注意:

  • 座標系統: PyAutoGUI 使用的座標系統原點 (0, 0) 位於螢幕左上角。
  • 圖片辨識: 圖片辨識功能依賴 Pillow 函式庫。
  • 安全機制: 建議啟用 pyautogui.FAILSAFE = True,以便在需要時強制停止程式。
  • 暫停時間: 可以調整 pyautogui.PAUSE 的值,以避免操作過快。
  • 例外處理: 建議使用 try-except 語法來處理可能發生的錯誤。
  • 官方文件: 更多詳細資訊請參考 PyAutoGUI 官方文件.

 

沒有留言:

張貼留言