一、安裝教程(Ubuntu 20.04)
# sudo apt-get install python3-pip
# pip3 install pyautogui
# sudo apt-get install scrot python3-tk python3-dev
一般這樣就可以了
二、使用教程
導(dǎo)入庫
import pyautogui
屏幕上的位置由X和Y坐標(biāo)表示。坐標(biāo)從0開始
獲取當(dāng)前鼠標(biāo)位置?
print(pyautogui.position())
獲取當(dāng)前屏幕的分辨率?
print(pyautogui.size())
判斷某個坐標(biāo)是否在屏幕上?
x=10
y=20
print(pyautogui.onScreen(x, y))
暫停2.5s:
防止程序出問題,一般要在執(zhí)行完后先停幾秒
pyautogui.PAUSE = 2.5
鼠標(biāo)移動:
移動時間為1s
#1、絕對移動
x = 200
y = 100
num_seconds = 1?
pyautogui.moveTo(x, y, duration=num_seconds) ?
#2、相對移動
xOffset = 30
yOffset = -50
num_seconds = 0.5
pyautogui.moveRel(xOffset, yOffset, duration=num_seconds)
鼠標(biāo)拖動:
按下鼠標(biāo)左鍵移動鼠標(biāo)。
#絕對移動
x = 200
y = 100
num_seconds= 1
pyautogui.dragTo(x, y, duration=num_seconds) ?
# 相對移動
xOffset = 30
yOffset = -50
num_seconds = 0.5
pyautogui.dragRel(xOffset, yOffset, duration=num_seconds)
鼠標(biāo)點擊:
# 將鼠標(biāo)移動到(moveToX,moveToY)位置,點擊鼠標(biāo)num_of_clicks次,每次點擊間隔
# secs_between_clicks秒
# button表示單擊方式,'left'左鍵單擊,'middle'中鍵單擊,'right'右鍵單擊
moveToX = 500
moveToY = 600
num_of_clicks = 1
secs_between_clicks = 1
pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button='left')
下面的函數(shù)都可以用click()代替,只是方便閱讀
moveToX = 10
moveToY = 20
# 右鍵單擊
pyautogui.rightClick(x=moveToX + 50, y=moveToY)??
# 中鍵單擊
pyautogui.middleClick(x=moveToX + 50, y=moveToY)??
# 左鍵雙擊
pyautogui.doubleClick(x=moveToX + 50, y=moveToY)??
# 左鍵三擊
pyautogui.tripleClick(x=moveToX + 50, y=moveToY)??
鼠標(biāo)滾動:
moveToX = 100
moveToY = 200
# 鼠標(biāo)在當(dāng)前位置向下滑動100格
pyautogui.scroll(clicks=-100)
# 鼠標(biāo)移動到(moveToX,moveToY)位置,然后向上滾動150格
pyautogui.scroll(clicks=150, x=moveToX, y=moveToY)
示例一:
# 鼠標(biāo)移動到(moveToX,moveToY)位置,鼠標(biāo)左鍵按下
pyautogui.mouseDown(x=moveToX, y=moveToY, button='left')
# 鼠標(biāo)移動到(moveToX,moveToY)位置,鼠標(biāo)右鍵松開(按下右鍵的情況下)
pyautogui.mouseUp(x=moveToX, y=moveToY, button='right')
# 鼠標(biāo)在當(dāng)前位置,按下中鍵
pyautogui.mouseDown(button='middle')
緩動/漸變函數(shù):
沒有什么實際作用,只是讓操作看起來更復(fù)雜
可以使用print(pyautogui.ease*?)函數(shù)查看
moveToX = 100
moveToY = 100
# 開始慢,結(jié)束快
pyautogui.moveTo(moveToX + 5 , moveToY+ 45, 2, pyautogui.easeInQuad) ?
# 開始快,結(jié)束慢 ??
pyautogui.moveTo(moveToX + 15, moveToY+ 35, 2, pyautogui.easeOutQuad)
# 快速開始和結(jié)束,中間緩慢
pyautogui.moveTo(moveToX + 25, moveToY+ 25, 2, pyautogui.easeInOutQuad) ?
# 最后反彈
pyautogui.moveTo(moveToX + 35, moveToY+ 15, 2, pyautogui.easeInBounce) ?
# 反復(fù)橫跳
pyautogui.moveTo(moveToX + 45, moveToY+ 5, 2, pyautogui.easeInElastic)
從鍵盤輸入文字:
# 在當(dāng)前位置輸入文字text,每個字符輸入間隔secs_between_keys秒
text = 'Hello world!\n'
secs_between_keys = 0.1
pyautogui.typewrite(message=text, interval=secs_between_keys) ?
# 在當(dāng)前位置按下鍵盤各種鍵
pyautogui.typewrite(['\t', 'a', 'b', 'c', 'left', 'backspace', 'enter', 'f1','\n'], interval=secs_between_keys)
查看所有支持的按鍵:
print(pyautogui.KEYBOARD_KEYS)
按下快捷鍵:
1、使用hotkey()按下快捷鍵,同時按兩個不松開
# ctrl+c 復(fù)制文字
pyautogui.hotkey('ctrl', 'c') ?
# ctrl+v 粘貼文字
pyautogui.hotkey('ctrl', 'v')
2、使用keyDown()按下鍵盤,keyUp()松開鍵盤
# 按下ctrl鍵
pyautogui.keyDown('ctrl')
# 按下v鍵,相當(dāng)文字粘貼
pyautogui.keyDown('v')
# 松開ctrl鍵盤
pyautogui.keyUp('ctrl')
3、使用press():按下再釋放
# ?按下shift鍵
pyautogui.keyDown('shift')
pyautogui.press('left')
pyautogui.press('left')
pyautogui.press('left')
# ?松開shift鍵
pyautogui.keyUp('shift')
# 按下三個left鍵,注意括號內(nèi)的數(shù)據(jù)格式
pyautogui.press(['left', 'left', 'left'])
# 按left鍵五次
pyautogui.press('left', presses=5)
4、hold():一直按住不松
# 按住shift
with pyautogui.hold('shift'):
????# 連續(xù)按left,然后松開shift
????pyautogui.press(['left', 'left', 'left'])
# 上面代碼功能和下面代碼實現(xiàn)功能相同
# 按下shift鍵
pyautogui.keyDown('shift')
pyautogui.press('left')
pyautogui.press('left')
pyautogui.press('left')
# 松開shift鍵
pyautogui.keyUp('shift')
消息框函數(shù):
向用戶展示信息或者需要和用戶互動
# 警告窗口
alert_result = pyautogui.alert('點擊確定返回字符串OK')
# 確認(rèn)窗口
confirm_result = pyautogui.confirm('點擊確定返回字符串OK,點擊取消返回字符串Cancel')
# 點擊ok保存輸入的文字,點擊Cancel返回None
prompt_result = pyautogui.prompt('輸入文字')
# 點擊ok保存輸入的密碼,點擊Cancel返回None
# default默認(rèn)文字,mask用什么符號代替輸入的密碼
password_result = pyautogui.password(text='', title='', default='', mask='*')
截圖函數(shù):
先安裝:sudo apt-get install scrot
官方說screenshot()函數(shù)大概需要100毫秒,但實際需要3秒左右且常常找不到圖片類似區(qū)域。
# 截屏返回result對象
result = pyautogui.screenshot()
# result是Image對象
print(type(result))
# 保存圖像
result.save('result1.jpg')
# 展示圖片
#result.show()
# imageFilename參數(shù)設(shè)置文件保存為止,在截屏前保存圖片到本地foo.png文件
# region設(shè)置截圖區(qū)域[x,y,w,h],以(x,y)為左上角頂點,截寬w,高h(yuǎn)的區(qū)域
result = pyautogui.screenshot(imageFilename='result2.jpg',region=[10,20,100,50])
圖像定位函數(shù):
locateOnScreen(image, grayscale=False):
在屏幕中,返回和image圖片最類似區(qū)域的坐標(biāo)(left, top, width, height),如果沒找到返回None。grayscale設(shè)置是否按照灰度查找。
locateCenterOnScreen(image, grayscale=False):
在屏幕中,返回和image圖片最類似區(qū)域的中心坐標(biāo)(x, y),如果沒找到返回None。
locateAllOnScreen(image, grayscale=False):
在屏幕中,返回和image圖片所有類似區(qū)域的坐標(biāo)(left, top, width, height)的生成器
locate(needleImage, haystackImage, grayscale=False):
在haystackImage中,返回和image圖片最類似區(qū)域的坐標(biāo)(left, top, width, height)。
locateAll(needleImage, haystackImage, grayscale=False):
在haystackImage中,返回和image圖片所有類似區(qū)域的坐標(biāo)(left, top, width, height)的生成器。
定位函數(shù)耗時很長,好幾秒鐘才行。加速它們的最好方法是傳遞一個region參數(shù)(一個(左、上、寬、高)的4整數(shù)元組)來只搜索屏幕的較小區(qū)域而不是全屏。但是這個region區(qū)域必須比待搜索截圖區(qū)域大,否則會引發(fā)錯誤:
result = pyautogui.locateOnScreen('result1.jpg', region=(0,0, 300, 400))
result = pyautogui.locate(needleImage='result1.jpg', haystackImage='result.jpg', confidence=0.5, region=(0,0, 300, 400))
可以傳遞grayscale=True給定位函數(shù)以提供輕微加速(大約30%左右)。但會降低圖像和屏幕截圖的顏色飽和度,加快定位速度,但可能會導(dǎo)致誤報匹配。文章來源:http://www.zghlxwxcb.cn/news/detail-488724.html
result_location = pyautogui.locateOnScreen('result.jpg', grayscale=True,confidence=0.6)
如果只需要驗證單個像素是否與給定像素匹配,可以調(diào)用該pixelMatchesColor()函數(shù),并將其表示的顏色的X坐標(biāo)、Y坐標(biāo)和RGB元組傳遞給它:文章來源地址http://www.zghlxwxcb.cn/news/detail-488724.html
# 顏色匹配
pyautogui.pixelMatchesColor(100, 200, (255, 255, 255))
# tolerance參數(shù)可以指定紅、綠、藍(lán)3種顏色誤差范圍
pyautogui.pixelMatchesColor(100, 200, (248, 250, 245), tolerance=10)
到了這里,關(guān)于Pyautogui--鍵盤&鼠標(biāo)控制工具的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!