turtle.write方法使用说明

本文详细介绍了Python的turtle模块中的turtle.write方法,用于在Turtle画布上以不同方式显示文本。通过调整参数,可以实现文本居中、换行、设置字体样式等效果。示例代码展示了如何创建文字环形排列和绘制花朵图案时添加文字。通过这些实例,读者可以更深入地掌握turtle模块在图形绘制中的应用。

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

turtle.write方法使用说明

关于turtle可参见 Python的turtle模块https://blog.csdn.net/cnds123/article/details/108252863

turtle.write()方法

在当前乌龟位置写入文本。如:

turtle.write("你好啊", align="center",font=("宋体",10,"normal"))

其中

你好啊 写入Turtle绘画屏幕的文字,是字符串格式,要有引号。

move(可选):在默认情况下,move为false。如果move为true,则笔将移动到右下角。

align(可选):可取值是left即左、center即中、right即右之一,是字符串格式。

font(可选):字体三元组(fontname、fontsize、fonttype),fontname即字体名称,fontsize即字体大小,fonttype即字体类型如:normal、bold、italic。。

例子

import turtle
info = "你输入的文字"
turtle.penup()
turtle.fd(-300)
turtle.pencolor('red')
for i in info:
    turtle.write(i, font=('宋体',40,'normal'))
    turtle.fd(60)
turtle.hideturtle()

运行效果如下:

绘制一朵小花的例子

import turtle as t
t.penup() 
t.fd(-200)
t.write("一朵小花\n", align="right", font=("楷体", 16, "bold"))

def draw_leaf():
    for i in range(2):
        for j in range(15):
            t.forward(5)
            t.right(6)
        t.right(90)
      
t.goto(0,-150)
t.left(90)
t.down()
t.forward(50)
t.fillcolor("green")
t.begin_fill()
draw_leaf()
t.end_fill()
t.forward(50)
t.right(270)
t.fillcolor("green")
t.begin_fill()
draw_leaf()
t.end_fill()
t.right(90)
t.forward(130)
t.fillcolor("red")
t.begin_fill()
for i in range(6):
    draw_leaf()
    t.right(60)
t.end_fill()

t.done()

运行效果如下:

如何使用turtle.write方法将文字显示为一个圆圈?

可近似地将画笔的运动轨迹看为一个正多边形。

根据多边形内角和公式:度数=(边数-2)*180,

那么,每次旋转的度数为:180-度数/角数=180-(边数-2)*180/边数。

易知,边数=角数=文字数

所以每次旋转的度数为:180-(文字数-2)*180/文字数=360/文字数。

例如

#将文字显示为一个圆圈
import turtle
text="你要显示的文字"
turtle.pu()
x=len(text)
for i in text:
    turtle.write(i,font='consolas')
    turtle.rt(360/x)
    turtle.pu()
    turtle.fd(30)
turtle.hideturtle()

运行效果如下:

#import ... 使用库中函数需要 库.函数 不会出现函数重名问题 #from .... import* 则可以直接使用函数名 #import .... as ....使代码量更少且不容易重名 import turtle #设置窗体大小和位置,4个参数中后两个可选为左上角坐标,setup函数不是必须的 turtle.setup(650,350,200,200) #绝对坐标 turtle.goto(x,y)海龟一开始在画面的中心(0,0),到达(x,y) #相对坐标 海龟当前运行的方向是前进方向 turtle.fd(d) 或turtle.forward(d) #后方是后退方向 turtle.bk(d) #左侧是左侧方向 turtle.circle(r,angle)以左侧的某个点为圆心向左侧 #右侧是右侧方向 # 画笔控制函数pen... # 一般成对出现: turtle.penup() 别名 turtle.pu() 不画 # turtle.pendown() turtle.pd() 画 # #画笔宽度设置后一直有效 turtle.pensize(width) 或 turtle.width(width) #画笔颜色 turtle.pencolor("purple")或 turtle.pencolor(0.63,0.13,0.94) 或 turtle.pencolor((0.63,0.13,0.94)) turtle.penup() turtle.fd(-250) turtle.pendown() turtle.pensize(25) turtle.pencolor("purple") #绝对角度 turtle角度坐标体系,类似数学平面直角坐标系,turtle.seth(angle)改变海龟行进角度,但不行进 或turtle.setheading() #相对角度 turtle.left(angle)向左改变角度 turtle.right(angle)向右改变角度 turtle.seth(-40) #turtle.circle(r,extent=None)绘制弧 默认圆心是左侧r距离的位置,弧度为360 -r右侧 for i in range(4): turtle.circle(40,80) turtle.circle(-40,80) turtle.circle(40,80/2) turtle.fd(40) turtle.circle(16,180) turtle.fd(40*2/3) turtle.done()
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学习&实践爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值