成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

怎么使用python的pyautogui控制鼠標(biāo)和鍵盤

這篇文章主要介紹了怎么使用python的pyautogui控制鼠標(biāo)和鍵盤的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇怎么使用python的pyautogui控制鼠標(biāo)和鍵盤文章都會(huì)有所收獲,下面我們一起來看看吧。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:主機(jī)域名、虛擬主機(jī)、營銷軟件、網(wǎng)站建設(shè)、花垣網(wǎng)站維護(hù)、網(wǎng)站推廣。

PyAutoGUI是一個(gè)純Python的GUI自動(dòng)化工具,其目的是可以用程序自動(dòng)控制鼠標(biāo)和鍵盤操作,多平臺(tái)支持(Windows,OS X,Linux)。

1、安裝

pip3 install pyautogui

2、pyautogui鼠標(biāo)操作樣例

import pyautogui# 獲取當(dāng)前屏幕分辨率screenWidth, screenHeight = pyautogui.size()# 獲取當(dāng)前鼠標(biāo)位置currentMouseX, currentMouseY = pyautogui.position()# 2秒鐘鼠標(biāo)移動(dòng)坐標(biāo)為100,100位置  絕對(duì)移動(dòng)pyautogui.moveTo(x=100, y=100,duratinotallow=2, tween=pyautogui.linear)#鼠標(biāo)移到屏幕中央。pyautogui.moveTo(screenWidth / 2, screenHeight / 2)# 鼠標(biāo)左擊一次#pyautogui.click()# x # y # clicks 點(diǎn)擊次數(shù)# interval點(diǎn)擊之間的間隔# button 'left', 'middle', 'right' 對(duì)應(yīng)鼠標(biāo) 左 中 右或者取值(1, 2, or 3)# tween 漸變函數(shù)pyautogui.click(x=None, y=None, clicks=1, interval=0.0, buttnotallow='left', duratinotallow=0.0, tween=pyautogui.linear)# 鼠標(biāo)相對(duì)移動(dòng) ,向下移動(dòng)#pyautogui.moveRel(None, 10)pyautogui.moveRel(xOffset=None, yOffset=10,duratinotallow=0.0, tween=pyautogui.linear)# 鼠標(biāo)當(dāng)前位置0間隔雙擊#pyautogui.doubleClick()pyautogui.doubleClick(x=None, y=None, interval=0.0, buttnotallow='left', duratinotallow=0.0, tween=pyautogui.linear)# 鼠標(biāo)當(dāng)前位置3擊#pyautogui.tripleClick()pyautogui.tripleClick(x=None, y=None, interval=0.0, buttnotallow='left', duratinotallow=0.0, tween=pyautogui.linear)#右擊pyautogui.rightClick()#中擊pyautogui.middleClick()#  用緩動(dòng)/漸變函數(shù)讓鼠標(biāo)2秒后移動(dòng)到(500,500)位置#  use tweening/easing function to move mouse over 2 seconds.pyautogui.moveTo(x=500, y=500, duratinotallow=2, tween=pyautogui.easeInOutQuad)#鼠標(biāo)拖拽pyautogui.dragTo(x=427, y=535, duratinotallow=3,buttnotallow='left')#鼠標(biāo)相對(duì)拖拽pyautogui.dragRel(xOffset=100,yOffset=100,duratinotallow=,buttnotallow='left',mouseDownUp=False)#鼠標(biāo)移動(dòng)到x=1796, y=778位置按下pyautogui.mouseDown(x=1796, y=778, buttnotallow='left')#鼠標(biāo)移動(dòng)到x=2745, y=778位置松開(與mouseDown組合使用選中)pyautogui.mouseUp(x=2745, y=778, buttnotallow='left',duratinotallow=5)#鼠標(biāo)當(dāng)前位置滾輪滾動(dòng)pyautogui.scroll()#鼠標(biāo)水平滾動(dòng)(Linux)pyautogui.hscroll()#鼠標(biāo)左右滾動(dòng)(Linux)pyautogui.vscroll()

3、pyautogui鍵盤操作樣例

#模擬輸入信息pyautogui.typewrite(message='Hello world!',interval=0.5)#點(diǎn)擊ESCpyautogui.press('esc')# 按住shift鍵pyautogui.keyDown('shift')# 放開shift鍵pyautogui.keyUp('shift')# 模擬組合熱鍵pyautogui.hotkey('ctrl', 'c')

4、按鍵支持

按鍵

說明

enter(或return 或 \n)

回車

esc

ESC鍵

shiftleftshiftright

左右SHIFT鍵

altleftaltright

左右ALT鍵

ctrlleftctrlright

左右CTRL鍵

tab (\t)

TAB鍵

backspacedelete

BACKSPACE 、DELETE鍵

pageuppagedown

PAGE UP 和 PAGE DOWN鍵

homeend

HOME 和 END鍵

updownleft,right

箭頭鍵

f1f2f3….

F1…….F12鍵

volumemutevolumedown,volumeup

有些鍵盤沒有

pause

PAUSE鍵

capslocknumlock,scrolllock

CAPS LOCK, NUM LOCK, 和 SCROLLLOCK 鍵

insert

INS或INSERT鍵

printscreen

PRTSC 或 PRINT SCREEN鍵

winleftwinright

Win鍵

command

Mac OS X command鍵

5、截屏

5.1、整個(gè)屏幕截圖并保存

im1 = pyautogui.screenshot()im1.save('my_screenshot.png')im2 = pyautogui.screenshot('my_screenshot2.png')

5.2、屏幕查找圖片位置并獲取中間點(diǎn)

#在當(dāng)前屏幕中查找指定圖片(圖片需要由系統(tǒng)截圖功能截取的圖)coords = pyautogui.locateOnScreen('folder.png')#獲取定位到的圖中間點(diǎn)坐標(biāo)x,y=pyautogui.center(coords)#右擊該坐標(biāo)點(diǎn)pyautogui.rightClick(x,y)

6、安全設(shè)置

import pyautogui#保護(hù)措施,避免失控pyautogui.FAILSAFE = True#為所有的PyAutoGUI函數(shù)增加延遲。默認(rèn)延遲時(shí)間是0.1秒。pyautogui.PAUSE = 0.5

7、實(shí)例說明(僅供學(xué)習(xí))

7.1、需求場(chǎng)景

大量的windows端上使用某公司的代理軟件,不時(shí)會(huì)出現(xiàn)不可用的情況,多次反饋某公司無果,可又不能不用,只能是定期去重啟一下。

7.2、思路

使用Python的pyautogui模塊替代手動(dòng)登陸。

7.3、實(shí)踐

1、由于國際鏈路問題,往往不能一次就成功登陸。再打開軟件時(shí)和點(diǎn)擊登陸時(shí)均可能出現(xiàn)幾次不等的登陸失敗提示,需要點(diǎn)擊OK按鈕確認(rèn)。

2、程序會(huì)嘗試進(jìn)行五輪的登陸操作(使用對(duì)應(yīng)的階段的圖片),期間檢測(cè)到成功登陸則退出。

3、建腳本加入到開機(jī)啟動(dòng)(至于重啟windows的任務(wù)就交給Ansible,將再其他文章進(jìn)行詳細(xì)說明)

# -*- coding: utf-8 -*-import osimport timeimport subprocessimport pyautogui

def kill_proc(description, proc_name):    try:        print('終止%s進(jìn)程...' % (description))        os.system('taskkill /IM %s /F' % (proc_name))    except Exception as reason:        print(e)def run_proc(description, proc_name):    try:        print('啟動(dòng)%s進(jìn)程...' % (description))        subprocess.Popen("%s" % (proc_name))        time.sleep(8)    except Exception as reason:        print('啟動(dòng)進(jìn)程失敗!!\n錯(cuò)誤的原因是:' + str(reason))        os._exit(0)def check_img(description, img_name):    try:        print('查找%s圖像' % (description))        button = pyautogui.locateOnScreen("%s" % (img_name))        print(button)        if description == 'Success':            return 1        if button:            print('點(diǎn)擊%s圖像' % (description))            button_center = pyautogui.center(button)            pyautogui.click(button_center)            time.sleep(1)    except BaseException:        print('沒有找到%s圖像' % (description))        return 0if __name__ == '__main__':    count = 1    kill_proc('911', 'Client.exe')    run_proc('911', 'Client.exe')    for i in range(5):        print('=====進(jìn)行第%s輪檢查=====' % (count))        count += 1        check_img('OK1', 'C:\\error_OK1.PNG')        check_img('OK2', 'C:\\error_OK2.PNG')        check_img('Login', 'C:\\Login.PNG')        time.sleep(5)        if check_img('Success', 'C:\\success.PNG'):            print('執(zhí)行成功!!')            break

怎么使用python的pyautogui控制鼠標(biāo)和鍵盤

關(guān)于“怎么使用python的pyautogui控制鼠標(biāo)和鍵盤”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“怎么使用python的pyautogui控制鼠標(biāo)和鍵盤”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前題目:怎么使用python的pyautogui控制鼠標(biāo)和鍵盤
當(dāng)前URL:http://jinyejixie.com/article28/pddsjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、企業(yè)建站商城網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司
鄄城县| 中阳县| SHOW| 沂南县| 景洪市| 册亨县| 黄陵县| 中卫市| 公安县| 武夷山市| 房山区| 阿图什市| 丰宁| 石城县| 东山县| 石嘴山市| 通山县| 太谷县| 西华县| 富蕴县| 平舆县| 新平| 哈尔滨市| 沙雅县| 本溪| 陈巴尔虎旗| 顺平县| 遂川县| 舟曲县| 库伦旗| 平原县| 涟源市| 鹿邑县| 衡阳市| 会昌县| 邹城市| 江津市| 剑河县| 湖南省| 新巴尔虎右旗| 缙云县|