通过python里面的pyautogui自动化控制鼠标和键盘操作
生活随笔
收集整理的這篇文章主要介紹了
通过python里面的pyautogui自动化控制鼠标和键盘操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
PyAutoGUI是一個純Python的GUI自動化工具,其目的是可以用程序自動控制鼠標和鍵盤操作,多平臺支持(Windows,OS X,Linux)。
?
安裝
pip3 install pyautoguipyautogui鼠標操作樣例
import pyautogui# 獲取當前屏幕分辨率 screenWidth, screenHeight = pyautogui.size()# 獲取當前鼠標位置 currentMouseX, currentMouseY = pyautogui.position()# 2秒鐘鼠標移動坐標為100,100位置 絕對移動 #pyautogui.moveTo(100, 100,2) pyautogui.moveTo(x=100, y=100,duration=2, tween=pyautogui.linear)#鼠標移到屏幕中央。 pyautogui.moveTo(screenWidth / 2, screenHeight / 2)# 鼠標左擊一次 #pyautogui.click() # x # y # clicks 點擊次數 # interval點擊之間的間隔 # button 'left', 'middle', 'right' 對應鼠標 左 中 右或者取值(1, 2, or 3) # tween 漸變函數 # pyautogui.click(x=None, y=None, clicks=1, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)# 鼠標相對移動 ,向下移動 #pyautogui.moveRel(None, 10) pyautogui.moveRel(xOffset=None, yOffset=10,duration=0.0, tween=pyautogui.linear)# 鼠標當前位置0間隔雙擊 #pyautogui.doubleClick() pyautogui.doubleClick(x=None, y=None, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)# 鼠標當前位置3擊 #pyautogui.tripleClick() pyautogui.tripleClick(x=None, y=None, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)#右擊 pyautogui.rightClick()#中擊 pyautogui.middleClick()# 用緩動/漸變函數讓鼠標2秒后移動到(500,500)位置 # use tweening/easing function to move mouse over 2 seconds. pyautogui.moveTo(x=500, y=500, duration=2, tween=pyautogui.easeInOutQuad)#鼠標拖拽 pyautogui.dragTo(x=427, y=535, duration=3,button='left')#鼠標相對拖拽 pyautogui.dragRel(xOffset=100,yOffset=100,duration=,button='left',mouseDownUp=False)#鼠標移動到x=1796, y=778位置按下 pyautogui.mouseDown(x=1796, y=778, button='left')#鼠標移動到x=2745, y=778位置松開(與mouseDown組合使用選中) pyautogui.mouseUp(x=2745, y=778, button='left',duration=5)#鼠標當前位置滾輪滾動 pyautogui.scroll() #鼠標水平滾動(Linux) pyautogui.hscroll() #鼠標左右滾動(Linux) pyautogui.vscroll()pyautogui鍵盤操作樣例
#模擬輸入信息 pyautogui.typewrite(message='Hello world!',interval=0.5) #點擊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)截屏
整個屏幕截圖并保存
im1 = pyautogui.screenshot() im1.save('my_screenshot.png')im2 = pyautogui.screenshot('my_screenshot2.png')屏幕查找圖片位置并獲取中間點
#在當前屏幕中查找指定圖片(圖片需要由系統截圖功能截取的圖) coords = pyautogui.locateOnScreen('folder.png') #獲取定位到的圖中間點坐標 x,y=pyautogui.center(coords) #右擊該坐標點 pyautogui.rightClick(x,y)安全設置
import pyautogui#保護措施,避免失控 pyautogui.FAILSAFE = True #為所有的PyAutoGUI函數增加延遲。默認延遲時間是0.1秒。 pyautogui.PAUSE = 0.5 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的通过python里面的pyautogui自动化控制鼠标和键盘操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python opencv Import
- 下一篇: python gui打包exe pyin