Python: Drawing in Turtle window says 'Not Responding'

杀马特。学长 韩版系。学妹 提交于 2019-12-13 03:49:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!