【问题标题】:Turtle Graphics: How to shift label upwards?Turtle Graphics:如何向上移动标签?
【发布时间】: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


【解决方案1】:

如果你想向上移动,那么将笔向上,向前移动,写文字,向后移动,放下宠物。

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.write(str(height)) while t.penup()
  • 如果不能在penup 时使用write,那么您可以在write 之前直接使用pendown,在write 之后直接使用penup
【解决方案2】:

您可以通过保存海龟的状态(它的位置和方向),在绘制标签之前将其向右移动一点,然后在之后恢复其状态来做到这一点。

这就是我的意思:

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

【讨论】:

    猜你喜欢
    • 2021-01-27
    • 2012-05-07
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    相关资源
    最近更新 更多