猿问

海龟图形屏幕没有响应

因此,我试图在屏幕上绘制一些文本,并且每当我按一下Turtle图形屏幕时,它都将变得无响应。当我尝试通过添加主循环来修复它时,它不会继续使用其余的代码。我看到应该添加的地方


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

我怎样才能解决这个问题?


慕码人2483693
浏览 188回答 1
1回答

慕盖茨4494581

本turtle.mainloop()不应该出现在一个子程序。一般来说,它应该是在海龟代码的页面上执行的最后一件事。即,字面上的最后一条语句或main()例程所做的最后一件事情。它将控制权交给tkinter的事件处理程序,在那里与乌龟的所有交互都是通过事件(按键,鼠标移动等)进行的。下面大致是我希望如何布置一个合适的海龟程序:from turtle import Turtle, Screen  # force Object-oriented interfaceSTYLE = ('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()
随时随地看视频慕课网APP

相关分类

Python
我要回答