turtle 库——绘图

本文介绍了Python的turtle库,包括如何通过setup函数设置窗口大小和位置,以及利用库中的画笔状态和运动函数进行图形创作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

窗体函数

t u r t l e . s e t u p ( w i d t h , h e i g h t , s t a r t x , s t a r t y ) \textcolor{red}{turtle.setup(width, height, startx, starty)} turtle.setup(width,height,startx,starty)
设置主窗体的大小和位置

参数描述
width窗口 宽 度 \textcolor{red}{宽度}
如果值是整数,表示的像素值
如果值是小数,表示窗口宽度与屏幕的比例;
height窗口 高 度 \textcolor{red}{高度}
如果值是整数,表示的像素值
如果值是小数,表示窗口高度与屏幕的比例;
startx窗口 左 侧 \textcolor{red}{左侧} 与屏幕左侧的像素距离
如果值是None
窗口位于屏幕水平中央;
starty窗口 顶 部 \textcolor{red}{顶部} 与屏幕顶部的像素距离
如果值是None
窗口位于屏幕垂直中央;

画笔状态函数

函数描述
pendown() 放 下 \textcolor{red}{放下} 画笔
turtle. p u ( ) \textcolor{red}{pu()} pu(), turtle.up()
penup() 提 起 \textcolor{red}{提起} 画笔
turtle. p d ( ) \textcolor{red}{pd()} pd(), turtle.down()
pensize(width)设置画笔线条的 粗 细 \textcolor{red}{粗细} 为指定大小
无参数输入时返回当前画笔宽度
turtle.width()
pencolor()设置 画 笔 颜 色 \textcolor{red}{画笔颜色}
无参数输入时返回当前画笔颜色
color()设置 画 笔 和 填 充 颜 色 \textcolor{red}{画笔和填充颜色}
无参数输入时返回当前画笔和背景颜色
begin_fill()图形 填 充 前 \textcolor{red}{填充前} ,调用该方法
end_fill()图形 填 充 结 束 \textcolor{red}{填充结束}
filling()返回 填 充 状 态 \textcolor{red}{填充状态} ,True 为填充,False 为未填充
clear() 清 空 \textcolor{red}{清空} 当前窗口,但当前画笔的 位 置 不 变 \textcolor{red}{位置不变}
reset() 清 空 \textcolor{red}{清空} 当前窗口,并 重 置 位 置 \textcolor{red}{重置位置} 等状态为默认值
screensize()设置 画 布 的 宽 度 、 高 度 和 背 景 颜 色 \textcolor{red}{画布的宽度、高度和背景颜色}
无参数输入时返回当前画布窗口的宽度和高度
hideturtle() 隐 藏 \textcolor{red}{隐藏} 画笔的 turtle 形状
showturtle() 显 示 \textcolor{red}{显示} 画笔的 turtle 形状
isvisible()如果 turtle 可见,则返回 True
write(str,font) 输 出 字 符 \textcolor{red}{输出字符}
font=(<字体名称>,<字体尺寸>,<字体类型>)

画笔运动函数

函数描述
forward()沿着当前方向 前 进 指 定 距 离 \textcolor{red}{前进指定距离}
turtle. f d \textcolor{red}{fd} fd(distance)
backward()沿着当前相反方向 后 退 指 定 距 离 \textcolor{red}{后退指定距离} 退
turtle. b k \textcolor{red}{bk} bk(distance)
right(angle) 右 旋 转 \textcolor{red}{右旋转} angle 角度
left(angle) 左 旋 转 \textcolor{red}{左旋转} angle 角度
goto(x,y)移动到绝对坐标 ( x , y ) \textcolor{red}{(x,y)} x,y
setx( )将当前 x 轴 \textcolor{red}{x 轴} x 移动到指定位置
sety( )将当前 y 轴 \textcolor{red}{ y 轴} y 移动到指定位置
setheading(angle)设置 当 前 朝 向 为 a n g l e 角 度 \textcolor{red}{当前朝向为 angle 角度} angle
turtle. s e t h \textcolor{red}{seth} seth(to_angle)
home()设置当前画笔位置为 原 点 \textcolor{red}{原点} ,朝向东。
circle(radius,e)绘制一个指定半径 r 和角度 e 的圆或 弧 形 \textcolor{red}{弧形}
circle(radius,steps=<边数>) x 边 形 \textcolor{red}{x 边形} x
dot(r,color)绘制一个指定半径 r 和颜色 color 的 有 色 圆 点 \textcolor{red}{有色圆点}
undo() 撤 销 \textcolor{red}{撤销} 画笔最后一步动作
speed()设置画笔的绘制 速 度 \textcolor{red}{速度} ,参数为 0-10 之间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值