Python實(shí)現(xiàn)自動(dòng)化調(diào)用鼠標(biāo)和鍵盤
自動(dòng)化鼠標(biāo),鍵盤調(diào)用程序用到的模塊是pyautogui。
pyautogui是一個(gè)Python第三方庫(kù),用于自動(dòng)化GUI操作。它可以模擬鼠標(biāo)和鍵盤的操作,以及屏幕截圖和圖像識(shí)別等功能。通過(guò)pyautogui,用戶可以編寫腳本來(lái)自動(dòng)化一些重復(fù)性的任務(wù),例如自動(dòng)化測(cè)試、數(shù)據(jù)錄入、圖像處理等。
pyautogui支持跨平臺(tái),可以在Windows、macOS和Linux等操作系統(tǒng)上運(yùn)行。它還提供了豐富的API,包括鼠標(biāo)和鍵盤操作、屏幕截圖、圖像識(shí)別、定時(shí)器等功能,可以滿足大部分自動(dòng)化需求。
寫一個(gè)簡(jiǎn)單的示例程序,實(shí)現(xiàn)鼠標(biāo)定位在屏幕中央和點(diǎn)擊的效果:
- import pyautogui
- pyautogui.moveTo(pyautogui.size().width / 2,
- pyautogui.size().height / 2)
- pyautogui.click()
和以前一樣,需要在終端里下載模塊:pip install pyautogui
程序源碼如下:
import pyautogui import time # 設(shè)置繪圖區(qū)域的大小和位置 x, y = 150, 150 width, height = 500, 500 # 設(shè)置繪制的圖形的顏色和線條寬度 color = 'blue' thickness = 5 # 獲取屏幕的大小 screenWidth, screenHeight = pyautogui.size() # 點(diǎn)擊屏幕上的指定位置打開(kāi)繪圖軟件 pyautogui.click(x, y) # 等待繪圖軟件加載完成 time.sleep(1) # 將鼠標(biāo)移動(dòng)到繪圖區(qū)域的左上角 pyautogui.moveTo(x, y) # 按下鼠標(biāo)左鍵并拖動(dòng)到繪圖區(qū)域的右上角 pyautogui.dragTo(x + width, y, duration=0.5, button='left') # 拖動(dòng)到繪圖區(qū)域的右下角 pyautogui.dragTo(x + width, y + height, duration=0.5, button='left') # 拖動(dòng)到繪圖區(qū)域的左下角 pyautogui.dragTo(x, y + height, duration=0.5, button='left') # 拖動(dòng)回繪圖區(qū)域的左上角,形成一個(gè)閉合圖形 pyautogui.dragTo(x, y, duration=0.5, button='left')
我們運(yùn)行試一下效果,實(shí)現(xiàn)鼠標(biāo)定位和拖拽效果,具有條件的同學(xué)可以嘗試一下。
再寫一個(gè)鍵盤調(diào)用的程序:
import pyautogui
import time
# 打開(kāi)記事本應(yīng)用程序
pyautogui.hotkey('win', 'r')
pyautogui.typewrite('notepad')
pyautogui.press('enter')
# 等待記事本應(yīng)用程序打開(kāi)
time.sleep(2)
# 打出英文名言,記得將輸入法切換為英文。
pyautogui.typewrite('To be, or not to be: that is the question.\nAsk not what your country can do for you, \nask what you can do for your country.\nI am the master of my fate,\n I am the captain of my soul.\n')
# 保存文件
pyautogui.hotkey('ctrl', 's')
time.sleep(2)
pyautogui.typewrite('test')
pyautogui.press('enter')
# 關(guān)閉記事本應(yīng)用程序
pyautogui.hotkey('alt', 'f4')
方法補(bǔ)充
Python PyAutoGUI 控制鼠標(biāo)、鍵盤和屏幕操作
1. 鼠標(biāo)控制
import pyautogui # 獲取屏幕尺寸 screen_width, screen_height = pyautogui.size() # 絕對(duì)坐標(biāo)移動(dòng) pyautogui.moveTo(500, 300, duration=0.5) # 移動(dòng)到(500,300) # 相對(duì)移動(dòng) pyautogui.move(100, -50) # 右移100px, 上移50px # 點(diǎn)擊操作 pyautogui.click() # 左鍵單擊 pyautogui.rightClick(600, 400) # 右鍵點(diǎn)擊指定位置 pyautogui.doubleClick() # 左鍵雙擊 # 拖拽操作 pyautogui.dragTo(800, 600, button='left') # 拖拽到目標(biāo)位置 pyautogui.drag(0, 200, duration=1) # 垂直向下拖拽
2. 鍵盤操作
# 文本輸入
pyautogui.write('Hello@世界!', interval=0.1) # 支持Unicode
# 按鍵操作
pyautogui.press('enter') # 按回車鍵
pyautogui.press(['tab', 'space']) # 按多個(gè)鍵
# 組合快捷鍵
pyautogui.hotkey('ctrl', 'c') # 復(fù)制
pyautogui.hotkey('ctrl', 'shift', 'esc') # 打開(kāi)任務(wù)管理器
# 長(zhǎng)按操作
pyautogui.keyDown('shift') # 按住Shift
pyautogui.press('4') # 輸入$符號(hào)
pyautogui.keyUp('shift') # 釋放Shift
3. 屏幕處理
# 全屏截圖
pyautogui.screenshot('fullscreen.png')
# 區(qū)域截圖
region = (100, 100, 500, 300) # (x, y, width, height)
pyautogui.screenshot('area.png', region=region)
# 像素分析
color = pyautogui.pixel(500, 300) # 獲取(500,300)處RGB值
if color == (255, 0, 0):
print("檢測(cè)到紅色像素")
# 圖像識(shí)別(需OpenCV)
try:
# 定位圖像中心點(diǎn)
x, y = pyautogui.locateCenterOnScreen('button.png', confidence=0.9)
pyautogui.click(x, y)
except pyautogui.ImageNotFoundException:
print("未找到目標(biāo)圖像")
到此這篇關(guān)于Python實(shí)現(xiàn)自動(dòng)化調(diào)用鼠標(biāo)和鍵盤的文章就介紹到這了,更多相關(guān)Python調(diào)用鼠標(biāo)和鍵盤內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pymongo insert_many 批量插入的實(shí)例
這篇文章主要介紹了pymongo insert_many 批量插入的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
Python程序打包工具py2exe和PyInstaller詳解
這篇文章主要介紹了Python程序打包工具py2exe和PyInstaller詳解,如果可以提前將程序打包成 Windows平臺(tái)的 .exe 文件或者是Linux下的 .sh 腳本,那么使用起來(lái)就會(huì)方便很多,需要的朋友可以參考下2019-06-06
Python 使用PIL中的resize進(jìn)行縮放的實(shí)例講解
今天小編就為大家分享一篇Python 使用PIL中的resize進(jìn)行縮放的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Python實(shí)現(xiàn)批量設(shè)置圖片背景為透明
我們?nèi)粘I钪兄谱鱌PT等教學(xué)資源時(shí),需要批量去除圖片背景,就可以使用 Python 的 rembg 庫(kù),下面我們就來(lái)看看如何操作rembg實(shí)現(xiàn)批量設(shè)置圖片背景為透明吧2024-11-11
Python爬蟲(chóng)小技巧之偽造隨機(jī)的User-Agent
這篇文章主要給大家介紹了關(guān)于Python爬蟲(chóng)小技巧之偽造隨機(jī)的User-Agent的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09

