1 回答

TA贡献1850条经验 获得超11个赞
本turtle.mainloop()不应该出现在一个子程序。一般来说,它应该是在海龟代码的页面上执行的最后一件事。即,字面上的最后一条语句或main()例程所做的最后一件事情。它将控制权交给tkinter的事件处理程序,在那里与乌龟的所有交互都是通过事件(按键,鼠标移动等)进行的。
下面大致是我希望如何布置一个合适的海龟程序:
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()
添加回答
举报