PyAutoGUI是一个Python模块,用于以编程方式控制鼠标和键盘,我们将利用Python中的PyAutoGUI库在桌面上自动化GUI应用程序,并在画图中自动操作鼠标进行画线操作。
首先我们先安装PyAutoGUI ,这里使用pip install pyautogui命令:
安装完成后,我们开始编写关键代码:
import pyautogui
import time
#休眠
time.sleep(5)
#执行点击操作
pyautogui.click()
#设置距离
distance = 400
while distance > 0:
#进行绘画操作
pyautogui.dragRel(distance, 0, duration=0.2)
distance = distance - 50
pyautogui.dragRel(0, distance, duration=0.2)
pyautogui.dragRel(-distance, 0, duration=0.2)
distance = distance - 50
pyautogui.dragRel(0, -distance, duration=0.2)
代码编写完成后我们使用命令执行这个文件名为py3_autogui.py,首先打开windows下的画图软件,然后打开cmd命令执行python py3_autogui.py,接着窗口最小化cmd命令窗口,然后看具体效果:
视频演示如下,运行程序后会自动操作鼠标在画图中进行绘画矩形的操作:
今天的学习就到这里,我们下节见。
下面的是我的公众号二维码图片,欢迎关注。