Python 海龟图形:如何向上移动标签?

Python 海龟图形:如何向上移动标签?,python,turtle-graphics,python-turtle,Python,Turtle Graphics,Python Turtle,我正在学习Python并试图编辑代码,该代码有以下错误: 每个高度标签的顶部部分由其条形图覆盖。您能否修改牵引杆的代码,稍微向上移动标签,但不改变杆?提示:在多边形填充序列期间无法绘制标签 我试图定义一个新函数,但没有成功。你能找到错误/编辑吗 def drawBar(t, height): """ Get turtle t to draw one bar, of height. """ t.begin_fill()

我正在学习Python并试图编辑代码,该代码有以下错误:

每个高度标签的顶部部分由其条形图覆盖。您能否修改牵引杆的
代码,稍微向上移动标签,但不改变杆?提示:在多边形填充序列期间无法绘制标签

我试图定义一个新函数,但没有成功。你能找到错误/编辑吗

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.penup()
t.forward(2)
t.write(str(height))
t.forward(-2)
t.pendown() 

最小工作码

import turtle

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.penup()
    t.forward(2)
    t.write(str(height))
    t.forward(-2)
    t.pendown()

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

t.forward(2)
t.right(90)
t.forward(3)

t.write(str(height))

t.forward(-3)
t.right(-90)
t.forward(-2)

t.pendown()

如果要向上移动,请设置画笔、向前移动、写入文本、向后移动、向下移动

t.penup()
t.forward(2)
t.write(str(height))
t.forward(-2)
t.pendown() 

最小工作码

import turtle

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.penup()
    t.forward(2)
    t.write(str(height))
    t.forward(-2)
    t.pendown()

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

t.forward(2)
t.right(90)
t.forward(3)

t.write(str(height))

t.forward(-3)
t.right(-90)
t.forward(-2)

t.pendown()

您可以保存海龟的状态(位置和方向),在绘制标签之前向右移动一点,然后恢复状态

我的意思是:

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)

    posn = t.position()          # Save position.
    heading = t.heading()        # Save heading.
    t.right(90)                  # Point to the right.
    t.forward(1)                 # Move over a little.
    t.write(height)              # Display the label.
    t.goto(posn)                 # Restore position.
    t.setheading(heading)        # Restore heading.

    t.right(90)
    t.forward(40)
    t.right(90)
    t.forward(height)
    t.left(90)
    t.end_fill()                 # stop filling this shape

您可以通过保存海龟的状态(位置和方向),在绘制标签之前将其向右移动一点,然后恢复其状态来完成此操作

我的意思是:

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)

    posn = t.position()          # Save position.
    heading = t.heading()        # Save heading.
    t.right(90)                  # Point to the right.
    t.forward(1)                 # Move over a little.
    t.write(height)              # Display the label.
    t.goto(posn)                 # Restore position.
    t.setheading(heading)        # Restore heading.

    t.right(90)
    t.forward(40)
    t.right(90)
    t.forward(height)
    t.left(90)
    t.end_fill()                 # stop filling this shape

您是否尝试在
t.end\u fill()之后使用
t.write()
?您是否尝试在
t.end\u fill()之后使用
t.write()
?这很简单,谢谢!我不知道你不能写(str(height))而t.penup()如果它不能
penup
时,那么你可以在
之前直接使用
pendown
,在
之后直接使用
penup
。这很简单,谢谢!我不知道你不能写(str(height))而t.penup()如果它不能
write
penup
时,那么你可以在
write
之前直接使用
pendown
,在
write
之后直接使用
penup