# PyAutoGUI
###### tags: `python`
:::danger
:exclamation::exclamation::exclamation: <font color=red>**Python 非常注重 Tab 、 空白 、 換行**</font>
:::
## 安裝PyAutoGUI
` pip install pyautogui`
# 修改fix位置為絕對路徑
```
# 修改EE_config.cfg (因人而異)
DataDictionary=C:\TPEX\TADS\fix\FIX43.xml
```
## 使用方式
`import pyautogui`
### 延遲所有PyAutoGUI函數
* pyautogui.PAUSE的默認值為0.1秒
* `pyautogui.FAILSAFE = 2.5` 延遲為2.5秒
### 一般功能
| 操作 | Code |
|:----------------------:|:--------------------------:|
| 獲取滑鼠的當前位置 | `pyautogui.position()` |
| 顯示器的大小 | `pyautogui.size()` |
| 檢查XY坐標是否在屏幕上 | `pyautogui.onScreen(x, y)` |
### 滑鼠
| 操作 | Code |
|:--------------------------------:|:---------------------------------------:|
| 獲取滑鼠的當前位置 | `pyautogui.position()` |
| 移動滑鼠 | `pyautogui.moveTo(x, y)` |
| 相對於當前位置移動滑鼠 | `pyautogui.moveRel(x,y)` |
| 按下左鍵移動滑鼠 | `pyautogui.dragTo(x,y)` |
| 按下左鍵,相對於當前位置移動滑鼠 | `pyautogui.dragRel(x,y)` |
| 單擊滑鼠 | `pyautogui.click()` |
| 移動並單擊滑鼠 | `pyautogui.click(x, y)` |
| 選擇要單擊的滑鼠 | `pyautogui.click(x, y, button='right')` |
| 設置點擊次數 | `pyautogui.click(x, y, clicks=2)` |
| 設置點擊之間的時間間隔 | `pyautogui.click(x, y,interval=0.5)` |
| 模擬右鍵點選 | `pyautogui.rightClick()` |
| 模擬中鍵點選 | `pyautogui.middleClick()` |
| 模擬左鍵雙擊 | `pyautogui.doubleClick()` |
| 在指定處按下滑鼠按鍵 | `pyautogui.mouseDown(x,y,button)` |
| 在指定處釋放滑鼠按鍵 | `pyautogui.mouseUp(x,y,button)` |
| 滾動滾輪 | `pyautogui.scroll(units)` |
### 鍵盤
| 操作 | Code |
|:--------------------:|:--------------------------------------------:|
| 按下並釋放_鍵 | `pyautogui.press(key)` |
| 按下並釋放多鍵 - 1 | `pyautogui.press(['left', 'right', 'left'])` |
| 按下並釋放多鍵 - 2 | `pyautogui.press('left', presses=3)` |
| 按下_鍵 | `pyautogui.keyDown(key)` |
| 釋放_鍵 | `pyautogui.keyUp(key)` |
| 按下熱鍵或鍵盤快捷鍵 | `pyautogui.hotkey('ctrl', 'v')` |
| 鍵入給定字串 | `pyautogui.write('Hello world!')` |
| 鍵入給定字元 | `pyautogui.typewrite(message)` |
| 鍵入給定字串 | `pyautogui.typewrite([key1,key2,key3])` |
:::success
[常用鍵盤代號](https://pyautogui.readthedocs.io/en/latest/keyboard.html)
:::
### 信息框功能
| 操作 | Code |
|:----:|:--------------------------------------------------------------------------:|
|顯示帶有文本和單個“OK”按鈕的簡單消息框| `pyautogui.alert(text='', title='', button='OK')` |
|顯示帶有“OK”和“Cancel”按鈕的消息框 | `pyautogui.confirm(text='', title='', buttons=['OK', 'Cancel'])` |
|顯示帶有文本輸入以及“OK”和“Cancel”按鈕的消息框。| `pyautogui.prompt(text='', title='' , default='')` |
|顯示帶有文本輸入以及“確定”和“取消”按鈕的消息框| `pyautogui.password()` |
### 屏幕截圖功能
| 操作 | Code |
|:--------------:|:-------------------------------:|
| 螢幕截圖 | `pyautogui.screenshot()` |
| 螢幕截圖並命名 | `pyautogui.screenshot('1.png')` |
| 螢幕截取特定範圍 | `pyautogui.screenshot(region=(0,0, 300, 400))` |
### 其他
| 操作 | Code |
|:--------------------:|:-----------------------------------------:|
| 查找圖像 | `pyautogui.locateOnScreen('_.PNG')` |
| 查找圖像所有位置 | `pyautogui.locateAllOnScreen('_.PNG')` |
| 查找圖像中間的XY坐標 | `pyautogui.locateCenterOnScreen('_.PNG')` |
| 獲得圖像區域的中心 | `pyautogui.center()` |
| 查找圖像(灰度匹配) |`pyautogui.locateOnScreen('_.png', grayscale=True)` |
| 查找圖像(像素匹配) | `pyautogui.pixel(100, 200)` |