答案:PyAutoGUI可通过控制鼠标在画图工具中绘制直线、矩形和近似圆形。首先打开画图程序并定位画布,使用moveTo()和dragTo()结合mouseDown()拖动绘图;绘制矩形时按路径移动鼠标并闭合,画圆则通过360个角度点模拟弧线;为提高精度,设置duration使移动平滑,提前选择笔刷并避免干扰;注意屏幕坐标适配、程序兼容性及运行时勿动鼠标,适用于简单自动化绘图演示。

使用 Python 的 PyAutoGUI 模块可以控制鼠标绘制图形,原理是移动鼠标并拖动以画出路径。虽然 PyAutoGUI 不是专为绘图设计的,但可以通过控制鼠标在绘图画板(如 Windows 画图工具)中绘制简单图形。
示例代码启动画图并等待几秒让你切换窗口(如果需要手动准备):
pyautogui.position() 手动查看鼠标当前位置pyautogui.moveTo() 和 pyautogui.dragTo() 控制鼠标移动和拖动。绘制矩形:
import pyautogui
import time
<h1>等待几秒打开画图</h1><p>time.sleep(3)</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/2257">
<img src="https://img.php.cn/upload/ai_manual/001/246/273/68b6b9d798f36854.png" alt="ListenLeap">
</a>
<div class="aritcle_card_info">
<a href="/ai/2257">ListenLeap</a>
<p>AI辅助通过播客学英语</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="ListenLeap">
<span>101</span>
</div>
</div>
<a href="/ai/2257" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="ListenLeap">
</a>
</div>
<p><span>立即学习</span>“<a href="https://pan.quark.cn/s/00968c3c2c15" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Python免费学习笔记(深入)</a>”;</p><h1>起始点 (x, y),宽度 w,高度 h</h1><p>x, y = 300, 300
w, h = 200, 100</p><p>pyautogui.moveTo(x, y)
pyautogui.mouseDown()
pyautogui.moveTo(x + w, y) # 上边
pyautogui.moveTo(x + w, y + h) # 右边
pyautogui.moveTo(x, y + h) # 下边
pyautogui.moveTo(x, y) # 左边闭合
pyautogui.mouseUp()
绘制圆形(近似):
由于不能直接画圆,可通过多个小线段模拟。import pyautogui
import math
import time
<p>def draw_circle(x, y, radius):
time.sleep(3) # 切换到画图
points = []
for i in range(360):
angle = math.radians(i)
px = x + radius <em> math.cos(angle)
py = y + radius </em> math.sin(angle)
points.append((px, py))</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">pyautogui.moveTo(points[0])
pyautogui.mouseDown()
for px, py in points:
pyautogui.moveTo(px, py, duration=0.01) # 每点之间缓慢移动
pyautogui.mouseUp()draw_circle(400, 400, 50)
duration 参数让移动更平滑,避免过快导致漏线pyautogui.click() 选择画笔工具基本上就这些。PyAutoGUI 适合做简单的自动化绘图演示或测试,不适合复杂图形设计。关键是先定位画布,再用鼠标拖动模拟手动画线。不复杂但容易忽略细节,比如等待时间、坐标准确性。
以上就是如何使用python pyautogui模块绘制图形?的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号