python 调用pyautogui 实时获取鼠标的位置、移动鼠标的方法
作者:lili414 时间:2021-01-03 05:25:29
PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,利用它可以实现自动化任务
本章介绍了许多不同函数,下面是快速的汇总参考:
moveTo(x,y)将鼠标移动到指定的 x、y 坐标。
moveRel (xOffset,yOffset)相对于当前位置移动鼠标。
dragTo(x,y)按下左键移动鼠标。
dragRel (xOffset,yOffset)按下左键,相对于当前位置移动鼠标。
click(x,y,button)模拟点击(默认是左键)。
rightClick() 模拟右键点击。
middleClick() 模拟中键点击。
doubleClick() 模拟左键双击。
mouseDown(x,y,button)模拟在 x、y 处按下指定鼠标按键。
mouseUp(x,y,button)模拟在 x、y 处释放指定键。
scroll (units)模拟滚动滚轮。正参数表示向上滚动,负参数表示向下滚动。
typewrite(message)键入给定消息字符串中的字符。
typewrite([key1,key2,key3])键入给定键字符串。
press(key)按下并释放给定键。
keyDown(key)模拟按下给定键。
keyUp(key)模拟释放给定键。
hotkey([key1,key2,key3])模拟按顺序按下给定键字符串,然后以相反的顺序释放。
screenshot() 返回屏幕快照的 Image 对象(参见第 17 章关于 Image 对象的信息)。
代码部分:
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....')
实际效果
import pyautogui
screenWidth, screenHeight = pyautogui.size()
currentMouseX, currentMouseY = pyautogui.position()
pyautogui.moveTo(100, 150)
pyautogui.click()
# 鼠标向下移动10像素
pyautogui.moveRel(None, 10)
pyautogui.doubleClick()
# 用缓动/渐变函数让鼠标2秒后移动到(500,500)位置
# use tweening/easing function to move mouse over 2 seconds.
pyautogui.moveTo(1800, 500, duration=2, tween=pyautogui.easeInOutQuad)
# 在每次输入之间暂停0.25秒
pyautogui.typewrite('Hello world!', interval=0.25) #输入文本
pyautogui.press('esc') #按下按键
pyautogui.keyDown('shift')
pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])
pyautogui.keyUp('shift')
pyautogui.hotkey('ctrl', 'c')
来源:https://www.cnblogs.com/lili414/p/9010043.html
标签:python,pyautogui,鼠标,位置,移动
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python两个内置函数 locals 和globals(学习笔记)
2022-12-04 10:13:07
Python使用 Beanstalkd 做异步任务处理的方法
2021-01-06 18:18:19
![](https://img.aspxhome.com/file/2023/1/75151_0s.png)
使用python实现微信小程序自动签到功能
2021-05-30 10:04:11
![](https://img.aspxhome.com/file/2023/0/64660_0s.jpg)
Python中的支持向量机SVM的使用(附实例代码)
2021-02-05 06:58:24
![](https://img.aspxhome.com/file/2023/6/75946_0s.png)
CSS网页布局开发时的常见问题小结
2009-08-13 12:17:00
windows 7安装ORACLE 10g客户端的方法分享
2012-07-11 15:36:18
Django跨域请求CSRF的方法示例
2021-07-13 21:09:34
基于python中staticmethod和classmethod的区别(详解)
2023-09-30 22:03:32
php对数字进行万、亿单位的转化
2023-06-24 08:34:32
django项目简单调取百度翻译接口的方法
2023-10-20 18:25:41
![](https://img.aspxhome.com/file/2023/0/64280_0s.png)
使用javascript修复浏览器中12个常见而又头痛的问题
2008-10-28 19:38:00
![](https://img.aspxhome.com/file/UploadPic/200810/28/20081028194555330s.jpg)
python 实现视频 图像帧提取
2023-12-30 19:29:40
可输入的select代码
2009-12-26 18:25:00
PHP PDOStatement::bindParam讲解
2023-06-05 05:47:28
Centos7.2 编译安装PHP7.0.2的步骤
2023-10-08 12:51:29
IPython 8.0 Python 命令行交互工具
2022-10-24 09:17:54
![](https://img.aspxhome.com/file/2023/0/87720_0s.png)
表格梳理解析python内置时间模块看完就懂
2023-10-21 08:10:27
浏览器的字体等宽空格
2008-08-28 12:25:00
![](https://img.aspxhome.com/file/UploadPic/20088/28/200882812297780s.jpg)
python使用KNN算法识别手写数字
2022-02-20 10:48:23
pycharm远程调试openstack的图文教程
2021-10-31 06:37:56
![](https://img.aspxhome.com/file/2023/6/87966_0s.png)