问题
I am trying to draw a square using Python turtle graphics using a for
loop. I am able to draw the square but the turtle window says 'Not Responding'. Adding my code below:
import turtle;
Bq = turtle.Turtle()
Bq.shape("turtle")
for i in range(4):
Bq.fd(100)
Bq.lt(90)
Bq.done()
回答1:
Have you tried this one?
I created a function named draw_square
:
def draw_square(some_turtle):
for i in range(1, 5):
some_turtle.forward(100)
some_turtle.right(90)
Then in your main function you can invoke the draw_square
function.
Example:
def draw_art():
window = turtle.Screen()
window.bgcolor("white")
#Create the turtle some_square - Draws a square
some_square = turtle.Turtle()
some_square.shape("turtle")
some_square.color("black")
some_square.speed(3)
some_square.right(20)
for i in range(1, 37):
draw_square(some_square)
some_square.right(10)
Lastly, call the draw_art
:
draw_art()
Hope that helps :)
回答2:
import turtle;
def drawSquare(TurtleName):
TurtleName.shape("turtle")
TurtleName.color("yellow")
TurtleName.speed(3)
for i in range(4):
TurtleName.fd(100)
TurtleName.rt(90)
turtle.mainloop()
bob = turtle.Turtle()
drawSquare(bob)
By adding turtle.mainloop() at the end I am able to avoid the Not Responding error.
来源:https://stackoverflow.com/questions/48637723/python-drawing-in-turtle-window-says-not-responding