python
主页 > 脚本 > python >

Python中turtle.write方法使用说明

2022-02-25 | 秩名 | 点击:

turtle.write方法使用说明

关于turtle可参见 Python的turtle模块:https://www.jb51.net/article/238830.htm

turtle.write()方法

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

1

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。。

例子

1

2

3

4

5

6

7

8

9

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()

运行效果如下:

绘制一朵小花的例子

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

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/文字数。

例如

1

2

3

4

5

6

7

8

9

10

11

#将文字显示为一个圆圈

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()

运行效果如下:

原文链接:https://blog.csdn.net/cnds123/article/details/113915180
相关文章
最新更新