# PyAutoGUI : 使用Python控制電腦

--

#20190601更新：使用圖片進行點擊動作

`pip install pyautogui`

`import pyautogui`

`#目前滑鼠坐標pyautogui.position()#目前螢幕解析度pyautogui.size()#(x,y)是否在螢幕上x, y = 122, 244pyautogui.onScreen(x, y)`

`#用num_seconds秒的时间把光标移动到(x, y)位置num_seconds = 1.2pyautogui.moveTo(x, y, duration=num_seconds)#用num_seconds秒的时间把光标的X轴（水平）坐标移动xOffset，Y轴（竖直）坐标向下移动yOffsetxOffset, yOffset = 50, 100pyautogui.moveRel(xOffset, yOffset, duration=num_seconds)#點擊左、中、右鍵pyautogui.rightClick(x=moveToX, y=moveToY)pyautogui.middleClick(x=moveToX, y=moveToY)pyautogui.doubleClick(x=moveToX, y=moveToY)pyautogui.tripleClick(x=moveToX, y=moveToY)#捲動pyautogui.scroll(clicks=amount_to_scroll, x=moveToX, y=moveToY)#移動滑鼠(若輸入None,則以目前滑鼠位置為基準)pyautogui.moveTo(100, 200)                 # 光标移动到(100, 200)位置pyautogui.moveTo(None, 500)                # 光标移动到(100, 500)位置pyautogui.moveTo(600, None)                # 光标移动到(600, 500)位置pyautogui.moveTo(100, 200, duration=2)     # 用2秒移动到(100, 200)位置#按住鼠标左键，把鼠标拖拽到(100, 200)位置pyautogui.dragTo(100, 200, button='left')#按住鼠标左键，用2秒钟把鼠标拖拽到(300, 400)位置pyautogui.dragTo(300, 400, 2, button='left')#按住鼠标右键，用2秒钟把鼠标拖拽到(30,0)位置pyautogui.dragTo(30, 0, 2, button='right')#點擊滑鼠pyautogui.click()#先移动到(100, 200)再點擊pyautogui.click(x=100, y=200, duration=2)#點擊滑鼠右鍵pyautogui.click(button='right')#雙擊左键pyautogui.click(clicks=2)#兩次單擊之间停0.25秒pyautogui.click(clicks=2, interval=0.25)#三擊右键pyautogui.click(button='right', clicks=2, interval=0.25)mouseDown：按下；ｍouseUp：鬆開#鼠标左键按下再松开pyautogui.mouseDown(); pyautogui.mouseUp() #按下鼠标右键pyautogui.mouseDown(button='right') #移动到(100, 200)位置，然后松开鼠标右键pyautogui.mouseUp(button='right', x=100, y=200)#向上滚动10格pyautogui.scroll(10)#向下滚动10格pyautogui.scroll(-10)#移动到(100, 100)位置再向上滚动10格pyautogui.scroll(10, x=100, y=100)`

`#输入Hello world!pyautogui.typewrite('Hello world!')#每次输入间隔0.25秒，输入Hello world!pyautogui.typewrite('Hello world!', interval=0.25)#輸入多個按鍵pyautogui.typewrite(['a', 'b', 'c', 'left', 'backspace', 'enter', 'f1'], interval=secs_between_keys)#按下ENTER键pyautogui.press('enter')#按下F1键pyautogui.press('f1')#按下左方向键pyautogui.press('left')#連續按左鍵pyautogui.press(['left', 'left', 'left'])#按下`shift`键pyautogui.keyDown('shift')pyautogui.press('left')pyautogui.press('left')pyautogui.press('left')#鬆開`shift`键pyautogui.keyUp('shift')#快捷鍵pyautogui.hotkey('ctrl', 'a') # 全選pyautogui.hotkey('ctrl', 'c') # 複製pyautogui.hotkey('ctrl', 'v') # 貼上pyautogui.hotkey('ctrl', 'alt', 'delete')#快捷鍵組合`

`#截圖pyautogui.screenshot()pyautogui.screenshot('foo.png')#開啟圖片顯示X坐標、Y坐標pyautogui.locateOnScreen('pyautogui/looks.png')#圖片中心點X坐標、Y坐標pyautogui.locateCenterOnScreen('pyautogui/looks.png')`

`#以Windows內建的計算機為例，實現1+1=2的自動化操作#首先，我們必須透過螢幕擷取工具，將計算機當中"1"和"+"的按鈕位置擷取成圖片檔#隨後，透過locateCenterOnScreen，進行讀取按鈕位置的動作(回傳的是座標位置)#搭配上.click指令，即可完成操作import pyautogui                       pyautogui.click((pyautogui.locateCenterOnScreen('auxillary_files/1key.png')))                       pyautogui.click((pyautogui.locateCenterOnScreen('auxillary_files/pluskey.png')))                       pyautogui.click((pyautogui.locateCenterOnScreen('auxillary_files/1key.png')))                       pyautogui.press('enter')`