如何在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()

I checked if the codes were in the wrong order, but I didn't find anything wrong. I also tried removing t1.ht(), but it wasn't the problem. How do I fix it?

评论
  • 语柳
    语柳 回复

    问题出在您的Screen对象上。您不必初始化它,也不必更新它,除非您使用示踪剂。只需删除此Screen对象,然后将循环更改为

    turtle.exitonclick() 
    

    以防止关闭窗口。