Python中turtle模块的write()函数如何插入换行符?

How to insert a line break in the write () function of the turtle module in Python?

作为练习,我使用 python 中的 turtle 模块生成柱状图。

使用以下代码实现了一个简单的条形图,但有一个细节我想改进。

import turtle

def generaVentana():
    w = turtle.Screen()
    w.title("Ejercicio 7, 8")
    w.bgcolor("lightgreen")

    return w

def generaTortuga():
    t = turtle.Turtle()
    t.pensize(2)

    return t

wn = generaVentana()
tess = generaTortuga()

xs = [48, 117, -200, 240, 160, 260, 220]

def draw_bar(t, height):
    if (height < 100):
        t.color("black", "green")
    elif (height >= 100 and height < 200):
        t.color("black", "yellow")
    elif (height >= 200):
        t.color("black", "red")
    t.begin_fill()
    t.left(90)
    t.forward(height)
    t.right(90)
    t.write("   " + str(height))
    t.forward(40)
    t.right(90)
    t.forward(height)
    t.left(90)
    t.end_fill()
    t.penup()
    t.forward(10)
    t.pendown()

for v in xs:
    draw_bar(tess, v)

wn.mainloop()

当定义一个带有负值的条时,它会生成,但是使用 write () 函数打印的条的大小我看到它显示在条的内部。

但是我想让它显示在栏的外面

并尝试在代码中使用换行符标记“\n”与高度返回字符串的值连接,但它不起作用。

有一些方法可以强制执行此行为,谢谢。

逻辑是文本以当前乌龟位置作为基线写入,因此文本总是出现在该位置上方。

当你处于消极的一面时,你需要在书写文本之前向下多移动一些距离。在绘制条形图的过程中这样做有点麻烦,所以我会把标签打印和绘制条形图分开,像这样:

def draw_bar(t, height):
    if height < 100:
        t.color("black", "green")
    elif height >= 100 and height < 200:
        t.color("black", "yellow")
    else: # no need to put a condition here
        t.color("black", "red")

    # add this block:
    t.left(90)
    p = t.position()
    t.penup()
    # when height is negative, move 14 more in that downward direction.
    t.forward(height - 14 * int(height < 0)) 
    t.write("   " + str(height))
    t.goto(p) 
    t.pendown()
    # end of added block

    t.begin_fill()
    # removed: t.left(90)
    t.forward(height)
    t.right(90)
    # removed: t.write("   " + str(height))
    t.forward(40)
    t.right(90)
    t.forward(height)
    t.left(90)
    t.end_fill()
    t.penup()
    t.forward(10)
    t.pendown()

顺便说一句:不需要最后的 elif,你可以只做 else。此外,您不需要在 if 条件周围加上括号。

您可能还需要考虑将打印的文本居中 到栏的中心。更改此行:

t.write("   " + str(height))

至:

t.right(90)
t.forward(20) # move to (horizontal) center of bar
t.write(str(height), align="center")
t.left(90)

我会控制您的字体大小,这样您就可以相对于当前字体移动而不是估计默认字体:

from turtle import Screen, Turtle

xs = [48, 117, -200, 240, 160, 260, 220]

FONT_SIZE = 12
FONT = ('Arial', FONT_SIZE, 'normal')

def generaVentana():
    screen = Screen()
    screen.title("Ejercicio 7, 8")
    screen.bgcolor("lightgreen")

    return screen

def generaTortuga():
    turtle = Turtle()
    turtle.pensize(2)
    turtle.penup()

    return turtle

def draw_bar(t, height):
    if height < 100:
        t.fillcolor("green")
    elif 100 <= height < 200:
        t.fillcolor("yellow")
    else:
        t.fillcolor("red")

    t.left(90)
    t.pendown()

    t.begin_fill()
    t.forward(height)
    t.right(90)
    t.forward(20)

    if height < 0:
        t.penup()
        position = t.position()  # save
        t.sety(t.ycor() - FONT_SIZE - t.pensize())

    t.write(height, align='center', font=FONT)

    if height < 0:
        t.setposition(position)  # restore
        t.pendown()

    t.forward(20)
    t.right(90)
    t.forward(height)
    t.end_fill()

    t.penup()
    t.left(90)
    t.forward(10)

wn = generaVentana()
tess = generaTortuga()

for value in xs:
    draw_bar(tess, value)

wn.mainloop()