欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Python:用于鼠标控制的 pyautogui 模块

最编程 2024-07-01 12:33:52
...

文章背景:PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,利用它可以实现自动化任务。pyautogui模块中包含了一些函数,可以模拟鼠标移动、按键和滚动鼠标滚轮。本文对鼠标控制的相关函数进行介绍。

1 确定鼠标位置

1.1 坐标轴系统

pyautogui的鼠标函数使用x,y坐标,原点在屏幕左上角,向右x坐标增加,向下y坐标增加,所有坐标都是正整数,没有负数坐标。

>>> import pyautogui
>>> screenWidth, screenHeight = pyautogui.size() # Get the size of the primary monitor.
>>> print(screenWidth, screenHeight)
1366 768

使用pyautogui.size()函数,获得屏幕的分辨率。根据屏幕分辨率的不同,返回值可能不同。

1.2 确定鼠标位置

>>> currentMouseX, currentMouseY = pyautogui.position() # Get the XY position of the mouse.
>>> print(currentMouseX, currentMouseY)
350 465

使用pyautogui.position()函数,确定鼠标当前的位置。

2 控制鼠标移动

pyautogui.moveTo(x,y[,duration = t]) 将鼠标移动到屏幕的指定位置

pyautogui.moveRel(x,y[,duration = t]) 相对于当前位置,移动鼠标。

duration为可选值,指定将鼠标移动到目标位置所需的秒数。

3 控制鼠标交互

3.1 点击鼠标

pyautogui.mouseDown()   #按下鼠标按键(左键)
pyautogui.mouseUp()     #释放鼠标按键(左键)

pyautogui.click()       #在当前光标位置,使用鼠标左键点击

pyautogui.click([x,y,button='left/right/middle'])  #在(x,y)处点击鼠标左键/右键/中键
                                                   #但不推荐使用这种方法,下面这种方法效果更好
                                                   #pyautogui.moveTo(x,y,duration=t)
                                                   #pyautogui.click()
pyautogui.doubleClick() #双击鼠标左键

pyautogui.rightClick()  #单击鼠标右键

pyautogui.middleClick() #单击鼠标中键

3.2 拖动鼠标

pyautogui.dragTo(x,y[,duration=t])      #将鼠标拖动到指定位置

pyautogui.dragRel(x,y[,duration=t])    #将鼠标拖动到相对当前位置的位置
                                                               #x,y:水平移动,垂直移动

3.3 滚动鼠标

pyautogui.scroll(x)        #控制窗口上下滚动(滚动发生在鼠标的当前位置)
                                       #正数表示向上滚动,负数表示向下滚动,

x代表一个整型参数,说明向上或向下滚动多少单位。单位的意义在每个操作系统和应用上不一样,需要自行尝试。

4 函数汇总

pyautogui.size()          # Get the size of the primary monitor.
pyautogui.position()      # Get the XY position of the mouse.

moveTo(x, y)              # Moves the mouse cursor to the given x and y coordinates.
moveRel(xOffset, yOffset) # Moves the mouse cursor relative to its current position.

mouseDown(x, y, button)   # Simulates pressing down the given button at the position x, y.
mouseUp(x, y, button)     # Simulates releasing the given button at the position x, y.
click(x, y, button)       # Simulates a click (left button by default).
doubleClick()             # Simulates a double left-button click.
rightClick()              # Simulates a right-button click.
middleClick()             # Simulates a middle-button click.

dragTo(x, y)              # Moves the mouse cursor while the left button is held down.
dragRel(xOffset, yOffset) # Moves the mouse cursor relative to its current position while the left button is held down.

scroll(units)             # Simulates the scroll wheel. A positive argument scrolls up; a negative argument scrolls down.

5 应用示例

5.1 现在鼠标在哪里?

在鼠标移动时,随时显示x, y坐标。

# from the book: Automate the Boring Stuff with Python
print('Press Ctrl-C to quit.')
try:
    while True:
        # Get and print the mouse coordinates.
        x, y = pyautogui.position()
        positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
        print(positionStr,end = '')
        print('\b'*len(positionStr),end='',flush = True)
except KeyboardInterrupt:
    print('\nDone.')

视频演示:http://mpvideo.qpic.cn/0bf2qqaz4aabzqaaocrot5pvdbgdt2cadhqa.f10002.mp4?

5.2 绘制正方形旋转图案

在window10的画图软件中,选中铅笔,拖动鼠标,绘制一个正方形旋转图案。

# Adapted from the book: Automate the Boring Stuff with Python
import pyautogui, time
time.sleep(5)
pyautogui.click()
distance = 100
while distance >0:
    pyautogui.dragRel(distance,0,duration=0.2)  #move right
    distance = distance - 5
    pyautogui.dragRel(0,distance,duration=0.2)  #move down
    pyautogui.dragRel(-distance,0,duration=0.2) #move left
    distance = distance - 5
    pyautogui.dragRel(0,-distance,duration=0.2) #move up
    
print("Done!")

视频演示:http://mpvideo.qpic.cn/0bf2lqaluaaagiach7rpwrpvaxgdxjoaboqa.f10002.mp4?

参考资料:

  1. PyAutoGUI使用(https://ddz.red/wROQJ
  2. PyAutoGUI——图形用户界面自动化(https://zhuanlan.zhihu.com/p/41662890
  3. Automate the Boring Stuff with Python(https://ddz.red/y9qF5
  4. Python编程快速上手—让繁琐工作自动化(https://ddz.red/AFTmO

推荐阅读