【发布时间】:2021-01-28 01:58:54
【问题描述】:
我正在学习 Python,正在尝试编辑出现以下错误的代码:
每个高度标签都被其条的顶部部分覆盖。您可以修改
drawBar代码,稍微向上移动标签但不改变条形吗?提示:在多边形填充序列期间不能绘制标签。
我曾尝试def 一个新功能,但没有奏效。你能找到错误/编辑吗?
def drawBar(t, height):
""" Get turtle t to draw one bar, of height. """
t.begin_fill() # start filling this shape
t.left(90)
t.forward(height)
t.write(str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.left(90)
t.end_fill() # stop filling this shape
xs = [48, 117, 200, 240, 160, 260, 220] # here is the data
maxheight = max(xs)
numbars = len(xs)
border = 10
wn = turtle.Screen() # Set up the window and its attributes
wn.setworldcoordinates(0-border, 0-border, 40*numbars+border, maxheight+border)
wn.bgcolor("lightgreen")
tess = turtle.Turtle() # create tess and set some attributes
tess.color("blue")
tess.fillcolor("red")
tess.pensize(3)
for a in xs:
drawBar(tess, a)
wn.exitonclick()
【问题讨论】:
-
你有没有尝试在
t.end_fill()之后使用t.write()?
标签: python turtle-graphics python-turtle