通过python里面的pyautogui自动化控制鼠标和键盘操作
生活随笔
收集整理的這篇文章主要介紹了
通过python里面的pyautogui自动化控制鼠标和键盘操作
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
PyAutoGUI是一個(gè)純Python的GUI自動(dòng)化工具,其目的是可以用程序自動(dòng)控制鼠標(biāo)和鍵盤操作,多平臺(tái)支持(Windows,OS X,Linux)。
?
安裝
pip3 install pyautoguipyautogui鼠標(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(100, 100,2) pyautogui.moveTo(x=100, y=100,duration=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, button='left', duration=0.0, tween=pyautogui.linear)# 鼠標(biāo)相對(duì)移動(dòng) ,向下移動(dòng) #pyautogui.moveRel(None, 10) pyautogui.moveRel(xOffset=None, yOffset=10,duration=0.0, tween=pyautogui.linear)# 鼠標(biāo)當(dāng)前位置0間隔雙擊 #pyautogui.doubleClick() pyautogui.doubleClick(x=None, y=None, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)# 鼠標(biāo)當(dāng)前位置3擊 #pyautogui.tripleClick() pyautogui.tripleClick(x=None, y=None, interval=0.0, button='left', duration=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, duration=2, tween=pyautogui.easeInOutQuad)#鼠標(biāo)拖拽 pyautogui.dragTo(x=427, y=535, duration=3,button='left')#鼠標(biāo)相對(duì)拖拽 pyautogui.dragRel(xOffset=100,yOffset=100,duration=,button='left',mouseDownUp=False)#鼠標(biāo)移動(dòng)到x=1796, y=778位置按下 pyautogui.mouseDown(x=1796, y=778, button='left')#鼠標(biāo)移動(dòng)到x=2745, y=778位置松開(與mouseDown組合使用選中) pyautogui.mouseUp(x=2745, y=778, button='left',duration=5)#鼠標(biāo)當(dāng)前位置滾輪滾動(dòng) pyautogui.scroll() #鼠標(biāo)水平滾動(dòng)(Linux) pyautogui.hscroll() #鼠標(biāo)左右滾動(dòng)(Linux) pyautogui.vscroll()pyautogui鍵盤操作樣例
#模擬輸入信息 pyautogui.typewrite(message='Hello world!',interval=0.5) #點(diǎn)擊ESC pyautogui.press('esc') # 按住shift鍵 pyautogui.keyDown('shift') # 放開shift鍵 pyautogui.keyUp('shift') # 模擬組合熱鍵 pyautogui.hotkey('ctrl', 'c')按鍵支持
| 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,?f3…. | F1…….F12鍵 |
| volumemute,?volumedown,volumeup | 有些鍵盤沒有 |
| pause | PAUSE鍵 |
| capslock,?numlock,scrolllock | CAPS LOCK, NUM LOCK, 和 SCROLLLOCK 鍵 |
| insert | INS或INSERT鍵 |
| printscreen | PRTSC 或 PRINT SCREEN鍵 |
| winleft,?winright | Win鍵 |
| command | Mac OS X command鍵 |
提示信息
alert
#pyautogui.alert('This is an alert box.','Test') pyautogui.alert(text='This is an alert box.', title='Test')option
#pyautogui.confirm('Shall I proceed?') pyautogui.confirm('Enter option.', buttons=['A', 'B', 'C'])- ?
password
a = pyautogui.password('Enter password (text will be hidden)') print(a)prompt
a = pyautogui.prompt('input message') print(a)截屏
整個(gè)屏幕截圖并保存
im1 = pyautogui.screenshot() im1.save('my_screenshot.png')im2 = pyautogui.screenshot('my_screenshot2.png')屏幕查找圖片位置并獲取中間點(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)安全設(shè)置
import pyautogui#保護(hù)措施,避免失控 pyautogui.FAILSAFE = True #為所有的PyAutoGUI函數(shù)增加延遲。默認(rèn)延遲時(shí)間是0.1秒。 pyautogui.PAUSE = 0.5 《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的通过python里面的pyautogui自动化控制鼠标和键盘操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python opencv Import
- 下一篇: python gui打包exe pyin