安裝
使用或調(diào)試pyautogui程序需要在有圖形界面的系統(tǒng)上,此處使用的是 Ubuntu 16.04 桌面版。
pip install python3–xlib
apt–get install scrot
apt–get install python3–tk
apt–get install python3–dev
pip install pyautogui
鼠標(biāo)操作
鼠標(biāo)移動
pyautogui 使用 x-y 坐標(biāo)系,左上角的坐標(biāo)是(0, 0)
獲取屏幕分辨率
screen_width, screen_height = pyautogui.size()
獲取當(dāng)前鼠標(biāo)坐標(biāo)
x, y = pyautogui.position()
使用絕對坐標(biāo)移動(moveTo)
順時針移動,畫十次方框,duration參數(shù)表示持續(xù)時間,即當(dāng)前點(diǎn)到達(dá)下一個點(diǎn)所需花費(fèi)的時間。
for i in range(10):
pyautogui.moveTo(300, 300, duration=0.25)
pyautogui.moveTo(400, 300, duration=0.25)
pyautogui.moveTo(400, 400, duration=0.25)
pyautogui.moveTo(300, 400, duration=0.25)
畫十次圓
screen_width, screen_height = pyautogui.size()
print('screen_width: %s\nscreen_height: %s' % (screen_width, screen_height))
r = 250 # 圓半徑
o_x = screen_width / 2 # 圓心X坐標(biāo)
o_y = screen_height / 2 # 圓心Y坐標(biāo)
pi = math.pi
for i in range(10):
for angle in range(0, 360, 20):
# 利用圓的參數(shù)方程
x = o_x + r * math.sin(angle * pi / 180)
y = o_y + r * math.cos(angle * pi / 180)
pyautogui.moveTo(x, y, duration=0.1)
使用相對坐標(biāo)移動(moveRel)
for i in range(10):
pyautogui.moveRel(100, 0, duration=0.25)
pyautogui.moveRel(0, 100, duration=0.25)
pyautogui.moveRel(-100, 0, duration=0.25)
pyautogui.moveRel(0, -100, duration=0.25)
實(shí)時獲取鼠標(biāo)位置坐標(biāo)
try:
while True:
x, y = pyautogui.position()
print(x, y)
except KeyboardInterrupt:
print('\nExit.')
鼠標(biāo)點(diǎn)擊
pyautogui.click(x=cur_x, y=cur_y, button='left')
-
x,y是要點(diǎn)擊的位置,默認(rèn)是鼠標(biāo)當(dāng)前位置 - button 是要點(diǎn)擊的按鍵,有三個可選值:
left,middle,right,默認(rèn)是left
在當(dāng)前位置點(diǎn)擊右鍵
pyautogui.click(button='right')
在指定位置點(diǎn)擊左鍵
pyautogui.click(100, 100)
其它函數(shù)
-
pyautogui.doubleClick(): 雙擊 -
pyautogui.rightClick(): 右擊 -
pyautogui.middleClick(): 中擊
鼠標(biāo)拖拽
使用絕對坐標(biāo)拖拽(dragTo)
# 畫一個回字
pyautogui.moveTo(300, 300, duration=0.25)
pyautogui.dragTo(400, 300, duration=0.25)
pyautogui.dragTo(400, 400, duration=0.25)
pyautogui.dragTo(300, 400, duration=0.25)
pyautogui.dragTo(300, 300, duration=0.25)
pyautogui.moveTo(200, 200, duration=0.25)
pyautogui.dragTo(500, 200, duration=0.25)
pyautogui.dragTo(500, 500, duration=0.25)
pyautogui.dragTo(200, 500, duration=0.25)
pyautogui.dragTo(200, 200, duration=0.25)
使用相對坐標(biāo)拖拽(dragRel)
# 畫一個回字
pyautogui.moveTo(300, 300, duration=0.25)
pyautogui.dragRel(100, 0, duration=0.25)
pyautogui.dragRel(0, 100, duration=0.25)
pyautogui.dragRel(-100, 0, duration=0.25)
pyautogui.dragRel(0, -100, duration=0.25)
pyautogui.moveTo(200, 200, duration=0.25)
pyautogui.dragRel(300, 0, duration=0.25)
pyautogui.dragRel(0, 300, duration=0.25)
pyautogui.dragRel(-300, 0, duration=0.25)
pyautogui.dragRel(0, -300, duration=0.25)
滾輪
使用函數(shù)scroll(),它只接受一個整數(shù),值為正則往上滾,值為負(fù)則往下滾。
pyautogui.scroll(200)
根據(jù)像素顏色定位按鈕位置
img = pyautogui.screenshot() # 截屏
img_color = img.getpixel((300, 300)) # 獲取坐標(biāo)顏色 (48, 10, 36)
is_matched = pyautogui.pixelMatchesColor(300, 300, (48, 10,36)) # 判斷屏幕坐標(biāo)的像素顏色是不是等于某個值
print(is_matched) # True
根據(jù)按鈕圖片定位按鈕位置
corn_locate = pyautogui.locateOnScreen('corn/settings.png') # 找到按鈕所在坐標(biāo),分別含義是按鈕左上角x坐標(biāo),左上角y坐標(biāo),x方向大小,y方向大小 (5, 560, 54, 54)
corn_center_x, corn_center_y = pyautogui.center(corn_locate) # 找到按鈕中心點(diǎn)
pyautogui.click(corn_center_x, corn_center_y) # 點(diǎn)擊按鈕
鍵盤操作
鍵盤按鍵
輸入普通字符串
pyautogui.typewrite('Hello, world!', 0.25) # 0.25表示每輸完一個字符串延時0.25秒
輸入特殊字符
| 代碼 | 按鍵 |
|---|---|
enter/return/\n
|
回車 |
esc |
ESC鍵 |
shiftleft/shiftright
|
左右SHIFT鍵 |
altleft/altright
|
左右ALT鍵 |
ctrlleft/ctrlright
|
左右CTRL鍵 |
tab/\t
|
TAB鍵 |
backspace/delete
|
BACKSPACE/DELETE鍵 |
pageup/pagedown
|
PAGE UP/PAGE DOWN鍵 |
home/end
|
HOME/END鍵 |
up/down/left/right
|
上下左右鍵 |
f1/f2/... |
F1/F2/... |
volumemute/volumedown/volumeup
|
?? |
pause |
PAUSE鍵 |
capslock/numlock/scrolllock
|
CAPS LOCK/NUM LOCL/SCROLL LOCK鍵 |
insert |
INSERT鍵 |
printscreen |
PRINT SCREEN鍵 |
winleft/winright
|
左右Win鍵 |
command |
Mac上的command鍵 |
pyautogui.click(100, 100)
pyautogui.typewrite('Hello, world!', 0.25)
pyautogui.typewrite(['enter', 'a', 'b', 'left', 'left', 'X', 'Y'], '0.1')
鍵盤的按下和釋放
-
keyDown(): 按下某個鍵 -
keyUp(): 松開某個鍵 -
press(): 一次完整的按鍵,前面兩個函數(shù)的結(jié)合
如關(guān)閉某個窗口(ALT + F4)
pyautogui.keyDown('altleft')
pyautogui.press('f4')
pyautogui.keyUp('altleft')
或者直接使用熱鍵函數(shù)
pyautogui.hotkey('altleft', 'f4')