python_GUI操作(鼠标、键盘)
生活随笔
收集整理的這篇文章主要介紹了
python_GUI操作(鼠标、键盘)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import pyautogui,time # 獲取當前屏幕分辨率 print('當前分辨率為:' + str(pyautogui.size())) # 移動鼠標 def moveMouse(): # moveTo 指定位置畫一個正方形5次 # moveRel 當前位置畫一個正方形5次 for i in range(5): pyautogui.moveTo(100, 100, duration=0.25) pyautogui.moveTo(200, 100, duration=0.25) pyautogui.moveTo(200, 200, duration=0.25) pyautogui.moveTo(100, 200, duration=0.25) # 點擊鼠標 def clickMouse():? # 點擊2,898這個位置 pyautogui.click(2, 898) # 其他點擊事件 # pyautogui.click(100,,150,button='left') 在這個左邊位置點擊左鍵 # pyautogui.click(100,,150,button='right') 在這個左邊位置點擊右鍵 # pyautogui.doubleclick() 雙擊操作,同理也可以用在右鍵和中鍵 # 拖動鼠標 def dragMouse():? # 在畫圖中實現一個正方形旋轉圖案 time.sleep(3) distance = 200 while distance>0: pyautogui.dragRel(distance, 0, duration=0.2) ? # 向右移動 distance = distance - 5 pyautogui.dragRel(0, distance, duration=0.2) ? # 向上移動 pyautogui.dragRel(-distance, 0, duration=0.2) ?# 向左移動 distance = distance - 5 pyautogui.dragRel(0, -distance, duration=0.2) # 向下移動 # 滾動鼠標 def scrollMouse():? time.sleep(3) pyautogui.scroll(10) # 獲取屏幕快照 def getpixel():? # 獲取當前位置的RGB像素顏色,可適用于自動化中頁面跳轉驗證 time.sleep(3) RGB = pyautogui.screenshot().getpixel((0,0)) print('位置(0,0)的RGB像素為:' + str(RGB)) # 使用 pixelMatchesColor()函數判斷返回是否一致 judgeRGB = pyautogui.pixelMatchesColor(0,0,(82,146,226)) print(judgeRGB) # 鍵盤輸入 def inputKeyboard():? # 鍵盤輸入,首先要鼠標確認位置 time.sleep(3) pyautogui.click(100, 100) pyautogui.typewrite('Hello world!') # 也可以單個字符輸入,最終結果是 axyb pyautogui.typewrite('a','b','left','x','y')
'''
鍵盤鍵字符串
‘a’,'b','c','A','B','C','1','2','3','!','@' ? ? ? ? ? ? ? ? ? ?-- 單個字符串的鍵
'enter' (or 'return' or '\n') ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??-- 回車鍵
'esc'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -- Esc鍵
'shiftleft','shiftright'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -- 左右Shift鍵
'altleft','altright'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -- 左右Shift鍵
'ctrlleft','ctrlright'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ?-- 左右ctrl鍵
'tab' (or '\t')? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -- Tab鍵
'backspace','delete'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? -- Backspace與Delete鍵
'pageup','pagedown'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? -- Page Up與Page Down鍵
'home','end'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -- Home鍵 與 End 鍵
'up','down','left','right' ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? -- 上下左右鍵
'f1','f2'..? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -- F1到F12鍵
'pause'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-- Pause鍵
'''
# 移動鼠標 # moveMouse() # 點擊鼠標 # clickMouse() # 拖動鼠標 # dragMouse() # 滾動鼠標 # scrollMouse() # 獲取屏幕快照,RGB像素 # getpixel() # 鍵盤輸入 inputKeyboard
'''
鍵盤鍵字符串
‘a’,'b','c','A','B','C','1','2','3','!','@' ? ? ? ? ? ? ? ? ? ?-- 單個字符串的鍵
'enter' (or 'return' or '\n') ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??-- 回車鍵
'esc'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -- Esc鍵
'shiftleft','shiftright'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -- 左右Shift鍵
'altleft','altright'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -- 左右Shift鍵
'ctrlleft','ctrlright'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ?-- 左右ctrl鍵
'tab' (or '\t')? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -- Tab鍵
'backspace','delete'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? -- Backspace與Delete鍵
'pageup','pagedown'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? -- Page Up與Page Down鍵
'home','end'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -- Home鍵 與 End 鍵
'up','down','left','right' ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? -- 上下左右鍵
'f1','f2'..? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -- F1到F12鍵
'pause'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-- Pause鍵
'''
# 移動鼠標 # moveMouse() # 點擊鼠標 # clickMouse() # 拖動鼠標 # dragMouse() # 滾動鼠標 # scrollMouse() # 獲取屏幕快照,RGB像素 # getpixel() # 鍵盤輸入 inputKeyboard
轉載于:https://www.cnblogs.com/peng-lan/p/9645588.html
總結
以上是生活随笔為你收集整理的python_GUI操作(鼠标、键盘)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nginx配置SSL后不能正常访问解决方
- 下一篇: websocket python爬虫_p