Pyautogui - Python 操作鼠標(biāo)鍵盤

安裝

使用或調(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')

博客更新地址

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ??JavaScript 與 HTML 之間的交互是通過事件實(shí)現(xiàn)的。 ??事件,就是文檔或?yàn)g覽器窗口中發(fā)生的一些特...
    霜天曉閱讀 3,716評論 1 11
  • 01 看了一篇關(guān)于地震的文章,標(biāo)題是,9年后,我還是沒有跑出去。 有一個男孩在朋友圈寫下一段話:08年地震,廢墟下...
    江無敵是33啊閱讀 1,596評論 0 2

友情鏈接更多精彩內(nèi)容