赞
踩
pip install pyautogui -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pyobjc-core
pip install pyobjc
pip install pyautogui
#sudo apt-get install scrot python3-tk python3-dev
pip install python3-xlib
pip install pyautogui
屏幕位置有X和Y直角坐标表示,X坐标从左侧的0开始,向右增加。与数学不同, Y坐标从顶部的0开始,向下增加。
±--------------------------+
| | Y increases
| | |
| 1920 x 1080 screen | |
| | V
| |
| |
±--------------------------+ 1919, 1079
import time
import pyautogui
print(pyautogui.size()) # 屏幕分辨率
try:
while True:
x, y = pyautogui.position() # 返回鼠标的坐标
# 打印当前鼠标位置坐标
print(f'鼠标坐标:x={x}, y={y}')
time.sleep(1)
# 捕获异常 KeyboardInterrupt:用户中断执行(通常是输入^C)
except KeyboardInterrupt:
print('已退出')
>>> pyautogui.onScreen(0, 0)
True
>>> pyautogui.onScreen(0, -1)
False
改moveTo()函数会将鼠标光标移至你传递的X和Y整数坐标。例如:
import pyautogui
# 在每次PyAutoGUI调用(具体的操作)后就会有2秒的暂停
pyautogui.PAUSE = 2
pyautogui.moveTo(100, 200) # moves mouse to X of 100, Y of 200.
pyautogui.rightClick() # 鼠标原地右键单击
pyautogui.moveTo(800, 800) # moves mouse to X of 800, Y of 800.
pyautogui.rightClick() # 鼠标原地右键单击
通常,鼠标光标会立即移动到新坐标。如果您希望鼠标逐渐移动到新位置,请在移动所需的持续时间(以秒为单位)中传递第三个参数。例如:
import pyautogui
pyautogui.moveTo(100, 200) # moves mouse to X of 100, Y of 200
pyautogui.rightClick() # 鼠标原地右键单击
pyautogui.moveTo(800, 800, 2) # moves mouse to X of 800, Y of 800 over 2 seconds
pyautogui.rightClick() # 鼠标原地右键单击
如果要将鼠标光标相对于其当前位置(相对位置)移动几个像素,请使用给move()功能。此函数的参数类似于moveTo().例如:
import pyautogui
# 在每次PyAutoGUI调用(具体的操作)后就会有2秒的暂停
pyautogui.PAUSE = 2
pyautogui.moveTo(140*2, 80*2) # moves mouse to X of 140*2, Y of 80*2.
pyautogui.rightClick() # 鼠标原地右键单击
# 从当前位置移动鼠标
pyautogui.move(188*2, 155*2) # move the mouse left 188*2, down 155*2 pixels.
pyautogui.rightClick() # 鼠标原地右键单击
pyautogui的dragTo()和drag()函数的参数与moveTo()和move()函数的参数相似。此外,他们还有一个button可以设置为left,的关键字middle,并且right在拖动时按住鼠标键不放。例如:
import pyautogui
# 在每次PyAutoGUI调用(具体的操作)后就会有2秒的暂停
pyautogui.PAUSE = 2
# button可以设为 left, middle和right
pyautogui.dragTo(35*2, 35*2, button='left') # drag mouse to X of 35*2, Y of 35*2 while holding down left mouse button
pyautogui.drag(500, 0, 5) # 按住鼠标左键,用5秒钟把鼠标相对于(35*2, 35*2)往右移动500
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。