Python包介绍
PyAutoGui -- 模拟操控鼠标键盘
鼠标位置实时获取
import os
import time
import pyautogui as pag
try:
while True:
print("Press Ctrl-C to end")
screenwidth,screenheight = pag.size()
print(screenwidth,screenheight)
x,y = pag.position()
posstr = "Position:" + str(x).rjust(4)+','+str(y).rjust(4)
print(posstr)
time.sleep(0.2)
os.system('cls')
except KeyboardInterrupt:
print("end......")
详细方法
1、安全设置
-
保护措施,避免失控
pyautogui.FAILSAFE = true
-
为所有的PyAutoGUI函数增加延迟。默认延迟时间是0.1秒
pyautogui.PAUSE = 0.5
-
True=>确定x,y在屏幕上
print(pyautogui.onScreen(0, 0))
-
Size(width=1920, height=1080)=>确定屏幕尺寸
print(pyautogui.size())
2、移动鼠标
-
绝对移动
pyautogui.moveTo(100, 200)
-
相对移动
pyautogui.moveRel(100, 200)
-
绝对拖拽鼠标
pyautogui.dragTo(100, 200, button='left')
-
相对拖拽鼠标
pyautogui.dragRel(30, 0, 2, button='right')
-
鼠标双击--leftClic、rightClick、middleClick、tripleClick
pyautogui.click(x=100, y=200, clicks=2, button=pyautogui.LEFT)
pyautogui.doubleClick(x=100, y=200, button=pyautogui.LEFT)
-
鼠标移动到(100,200),向上滚10次,点击
pyautogui.scroll(10, x=100, y=100)
-
鼠标按下和抬起
pyautogui.mouseDown()
pyautogui.mouseUp()
3、键盘输入
-
输入字符
pyautogui.typewrite('Hello world!')
-
按下键,抬起键和press(可以传列表)
pyautogui.keyDown('shift') -- hold down the shift key
pyautogui.press('left') -- press the left arrow key
pyautogui.press('left') -- press the left arrow key
pyautogui.press('left') -- press the left arrow key
pyautogui.keyUp('shift') -- release the shift key
- 组合热键
pyautogui.hotkey('ctrl', 'a')
pyautogui.hotkey('ctrl', 'c')
其他
1、提示信息
-
提示窗口
pyautogui.alert(text='This is an alert box.', title='Test')
-
选择窗口
a = pyautogui.confirm('Enter option.', buttons=['A', 'B', 'C'])
-
密码输入窗口
a = pyautogui.password('Enter password (text will be hidden)')
-
文本输入窗口
a = pyautogui.prompt('input message')
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~