相关知识推荐阅读:
先给出一部分代码展示
import pyautogui
import time
# 设置窗口内的起始和终止位置
start_x, start_y = 300, 400
end_x, end_y = 500, 600
# 将鼠标移动到起始位置并按下左键
pyautogui.mouseDown(button='left', x=start_x, y=start_y)
time.sleep(0.1)
# 拖动鼠标到目标位置
pyautogui.moveTo(x=end_x, y=end_y, duration=0.5)
# 松开左键
pyautogui.mouseUp(button='left')
import pyautogui
import time
# 获取屏幕尺寸
screenWidth, screenHeight = pyautogui.size()
# 计算屏幕中间位置
start_x, start_y = screenWidth // 2, screenHeight // 2
end_x, end_y = start_x + 100, start_y # 向右拖动100像素
# 按下左键并移动到起始位置
pyautogui.mouseDown(button='left', x=start_x, y=start_y)
time.sleep(0.1)
# 拖动鼠标到目标位置
pyautogui.moveTo(x=end_x, y=end_y, duration=0.5)
# 松开左键
pyautogui.mouseUp(button='left')
pyautogui 是一个用于自动化控制鼠标和键盘的 Python 库,可以在脚本中模拟人类的鼠标和键盘操作
根据上述Demo,主要讲解mouseDown、moveTo 和 mouseUp 三个函数
pyautogui.mouseDown()基本的语法如下:
pyautogui.mouseDown(x=None, y=None, button='left')
对应的参数如下:
pyautogui.moveTo()pyautogui.moveTo(x, y, duration=0, tween=pyautogui.linear)
pyautogui.easeInOutQuad 等其他缓动函数pyautogui.mouseUp()pyautogui.mouseUp(x=None, y=None, button='left')