Python 如何在方框前显示文字?
How to display texts in front of the box in Python?
我创建了一个框和一个文本,我希望文本显示在框的前面。但是当我尝试这样做时,我看不到它,因为它在盒子后面。
import turtle
wn= turtle.Screen()
style = ("Courier", "36", "bold")
#Box -------------------------- ([Text] Box)
b1 = turtle.Turtle()
b1.color("black")
b1.shape("square")
b1.speed(0)
b1.shapesize(stretch_wid=5, stretch_len=10)
b1.penup()
b1.goto(-400, -150)
#Text ------------------------ "[Text]"
t1= turtle.Turtle()
t1.speed(0)
t1.color("white")
t1.ht()
t1.penup()
t1.goto(-400, -150)
t1.write("[Text]", font=style, align = "center")
#Main loop
while True:
wn.update()
我检查了代码是否顺序错误,但我没有发现任何错误。我也尝试删除 t1.ht()
,但这不是问题所在。我该如何解决?
问题出在您的 Screen 对象上。您不必初始化它,也不必更新它,除非您使用跟踪器。只需删除此 Screen 对象并将末尾的循环更改为
turtle.exitonclick()
防止关闭window.
shape()
用于定义乌龟的形状,而不是在canvas上画方框。因为 turtle
总是在最上面,所以文本隐藏在海龟后面。
要绘制方框,您必须使用 forward()
、left()
/right()
和 begin_fill()
/end_fill()
import turtle
# move
turtle.penup()
turtle.goto(-400, -150)
# box
turtle.color('black')
turtle.begin_fill()
for x in range(2):
turtle.forward(300)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.end_fill()
# move
turtle.penup()
turtle.goto(-250, -125)
# text
turtle.color("white")
turtle.write("[Text]", font=("Courier", "36", "bold"), align="center")
#turtle.goto(0, 0)
turtle.exitonclick()
结果
下面是一种混合了您尝试做的和@furas 推荐的方法的方法。它使用stamping而不是drawing来简化问题:
from turtle import Screen, Turtle
FONT_SIZE = 36
FONT = ('Courier', FONT_SIZE, 'bold')
screen = Screen()
textbox = Turtle()
textbox.hideturtle()
textbox.color('black')
textbox.shape('square')
textbox.shapesize(stretch_wid=5, stretch_len=10)
textbox.penup()
textbox.goto(-300, -150)
textbox.stamp()
textbox.color('white')
textbox.goto(-300, -150 - FONT_SIZE/2) # center vertically based on font size
textbox.write("[Text]", align='center', font=FONT)
screen.mainloop()
textbox
turtle 可以重复使用和改变形状以绘制其他文本框。通过一些思考和猜测,您可以根据字体大小和文本本身计算出合理的文本框大小。
我创建了一个框和一个文本,我希望文本显示在框的前面。但是当我尝试这样做时,我看不到它,因为它在盒子后面。
import turtle
wn= turtle.Screen()
style = ("Courier", "36", "bold")
#Box -------------------------- ([Text] Box)
b1 = turtle.Turtle()
b1.color("black")
b1.shape("square")
b1.speed(0)
b1.shapesize(stretch_wid=5, stretch_len=10)
b1.penup()
b1.goto(-400, -150)
#Text ------------------------ "[Text]"
t1= turtle.Turtle()
t1.speed(0)
t1.color("white")
t1.ht()
t1.penup()
t1.goto(-400, -150)
t1.write("[Text]", font=style, align = "center")
#Main loop
while True:
wn.update()
我检查了代码是否顺序错误,但我没有发现任何错误。我也尝试删除 t1.ht()
,但这不是问题所在。我该如何解决?
问题出在您的 Screen 对象上。您不必初始化它,也不必更新它,除非您使用跟踪器。只需删除此 Screen 对象并将末尾的循环更改为
turtle.exitonclick()
防止关闭window.
shape()
用于定义乌龟的形状,而不是在canvas上画方框。因为 turtle
总是在最上面,所以文本隐藏在海龟后面。
要绘制方框,您必须使用 forward()
、left()
/right()
和 begin_fill()
/end_fill()
import turtle
# move
turtle.penup()
turtle.goto(-400, -150)
# box
turtle.color('black')
turtle.begin_fill()
for x in range(2):
turtle.forward(300)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.end_fill()
# move
turtle.penup()
turtle.goto(-250, -125)
# text
turtle.color("white")
turtle.write("[Text]", font=("Courier", "36", "bold"), align="center")
#turtle.goto(0, 0)
turtle.exitonclick()
结果
下面是一种混合了您尝试做的和@furas 推荐的方法的方法。它使用stamping而不是drawing来简化问题:
from turtle import Screen, Turtle
FONT_SIZE = 36
FONT = ('Courier', FONT_SIZE, 'bold')
screen = Screen()
textbox = Turtle()
textbox.hideturtle()
textbox.color('black')
textbox.shape('square')
textbox.shapesize(stretch_wid=5, stretch_len=10)
textbox.penup()
textbox.goto(-300, -150)
textbox.stamp()
textbox.color('white')
textbox.goto(-300, -150 - FONT_SIZE/2) # center vertically based on font size
textbox.write("[Text]", align='center', font=FONT)
screen.mainloop()
textbox
turtle 可以重复使用和改变形状以绘制其他文本框。通过一些思考和猜测,您可以根据字体大小和文本本身计算出合理的文本框大小。