乌龟图形屏幕没有响应

Turtle graphic screen not responding

所以我试图在屏幕上绘制一些文本,但每次我按下海龟图形屏幕时它都没有响应。当我尝试通过添加主循环来修复它时,它不会继续执行其余代码。我看到了我应该添加的地方

done()

在块的末尾,但 python 说它不存在,我尝试放置 turtle.done() 但什么也没有。

代码如下:

def draw_robot(choice_robot,robots):
    stats = robots[choice_robot]
    style = 'Arial',14,'bold'
    t.setheading(-90)
    t.write('Name: '+choice_robot,font=style,align = 'center')
    t.forward(25)
    t.write('Battery: '+stats[0],font=style,align = 'center')
    t.forward(25)
    t.write('Intelligence: '+stats[1],font=style,align = 'center')
    turtle.mainloop()

我该如何解决这个问题?

turtle.mainloop() 不应出现在子例程中。一般应该是一页海龟代码最后执行的事情。即,字面意思是最后一条语句或 main() 例程所做的最后一件事。它将控制权交给 tkinter 的事件处理程序,在该事件处理程序中,与 turtle 的所有交互都是通过事件(击键、鼠标移动等)

下面是我期望的适当 turtle 程序的大致布局:

from turtle import Turtle, Screen  # force Object-oriented interface

STYLE = ('Arial', 14, 'bold')

def draw_robot(choice_robot, robots):
    stats = robots[choice_robot]

    t.setheading(-90)
    t.write('Name: ' + choice_robot, font=STYLE, align='center')
    t.forward(25)
    t.write('Battery: ' + stats[0], font=STYLE, align='center')
    t.forward(25)
    t.write('Intelligence: ' + stats[1], font=STYLE, align='center')

screen = Screen()

t = Turtle()

my_choice_robot = None  # whatever ...
my_robots = None  # whatever ...

draw_robot(my_choice_robot, my_robots)

screen.mainloop()