Python学习笔记(七):Turtle绘图(3)[write()函数]

write()函数

如果我想画一幅画,并且在这幅画里写字,该怎么办呢?

拟定代码如下:

import turtle as t
for i in range(1,10):
    t.circle(100,360,i)
    t.home()
print("Oh!It is so cool!")

结果:

这看起来不太妙,因为文字和图画输出在了两个界面上。

该怎么解决这个问题呢?

我们就要调用write()函数。

Python对于write()函数的介绍

翻译:

write(arg,move=false,align='left',font=('arial',8,'normal'))

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

arg--信息,将写入Turtle绘画屏幕。

move(可选)--真/假。

align(可选)--字符串“左(left)”、“中(center)”或“右(right)”之一。

font(可选)--三个字体(fontname、fontsize、fonttype)。

写入文本 - arg的字符串表示形式 - 当前

根据“对齐”(“左”、“中”或“右”)定位乌龟以及给定的字体。

如果move为true,则笔将移动到右下角。

在默认情况下,move为false。

实践方案

#上文例子

import turtle as t
for i in range(1,10):
    t.circle(100,360,i)
    t.home()
t.rt(90)
t.pu()
t.fd(30)#为了不与图像重合,将小乌龟向下移动后写字
t.write("Oh!It is so cool!",False,'center')
t.fd(50)#为了小乌龟不挡住字,将小乌龟再次向下移动

结果:

Oh ! It is so cool ! 

猜你喜欢

转载自blog.csdn.net/Commander_WingT/article/details/88748970