简体   繁体   中英

Turtle graphic screen not responding

So I'm trying to draw some text on the screen and everytime I press the turtle graphic screen it becomes unresponsive. When I tried fixing it by adding the mainloop it won't continue with rest of the code. I saw somewhere I should add

done()

at the end of the block but python says it doesn't exist and I tried putting turtle.done() but nothing.

Here is the code:

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

how can i fix this?

The turtle.mainloop() should not appear in a subroutine. Generally, it should be the last thing executed on a page of turtle code. Ie, either literally the last statement or the last thing a main() routine does. It turns control over to tkinter's event handler where upon all interaction with turtle is through events (key strokes, mouse motion, etc.)

Below is roughly how I would expect a proper turtle program to be laid out:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM