網站首頁 編程語言 正文
使用Python的第三方庫pyautogui,PyAutoGUI是一個純Python的GUI自動化工具,其目的是可以用程序自動控制鼠標和鍵盤操作,多平臺支持(Windows,OS X,Linux)。
安裝
pip install pyautogui
pyautogui鼠標操作樣例
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()
Python獲取鼠標實時位置具體實現
import time import pyautogui as pag try: while True: #獲取屏幕分辨率 screenWidth, screenHeight = pag.size() #獲取鼠標位置 x, y = pag.position() #打印分辨率和鼠標位置 print("Screen size: (%s %s), Position : (%s, %s)\n" % (screenWidth, screenHeight, x, y)) #間隔一秒顯示位置 time.sleep(1) except KeyboardInterrupt: print('end')
結果展示
屏幕的分辨率1920×1080,間隔1s移動鼠標后的結果:
總結
原文鏈接:https://blog.csdn.net/qq_36584673/article/details/117777658
相關推薦
- 2022-08-04 react使用mobx封裝管理用戶登錄的store示例詳解_React
- 2022-03-27 Android使用kotlin實現多行文本上下滾動播放_Android
- 2022-07-06 python函數運行內存時間等性能檢測工具_python
- 2022-03-28 python?Pandas中數據的合并與分組聚合_python
- 2022-05-17 Error running ‘myToncat‘: Address localhost:8080 i
- 2022-03-23 如何使用Redis實現電商系統的庫存扣減_Redis
- 2022-10-19 python用opencv將標注提取畫框到對應的圖像中_python
- 2022-05-13 當你敲完Hello World后的第一步——C語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支