简体   繁体   中英

when i run the code, the python turtle graphics stop responding

when i run the code the input window shows and i enter the number of squares but after that, python turtle graphics stop responding and then desapear without showing anything

#
import turtle
from random import randint
turtle.tracer(0)
turtle.bgcolor('black')
#Takes user input to decide how many squares are needed
f=int(input("How many squares do you want?"))

i=1

x=65

#Draws the desired number of squares.
while i < f:
    i=i+1
    x=x*1.05
    while x<400 :
      r=randint(0,255)
      g=randint(0,255)
      b=randint(0,255)
      turtle.colormode(255)
      turtle.pencolor(r,g,b)
      turtle.bk(x)
      turtle.rt(90)
      turtle.bk(x)
      turtle.rt(90)
      turtle.bk(x)
      turtle.rt(90)
      turtle.bk(x)
      turtle.rt(90)
      turtle.up()
      turtle.rt(9)
      turtle.down()
turtle.update()
turtle.mainloop()

Just move turtle.update() in the loop and add break , so after the number of squares are drawn, the window stops updating.

import turtle
from random import randint
turtle.tracer(0)
turtle.bgcolor('black')
#Takes user input to decide how many squares are needed
f=int(input("How many squares do you want?"))

i=1

x=65

#Draws the desired number of squares.
while i < f:
    i=i+1
    x=x*1.05
    while x<400 :
      r=randint(0,255)
      g=randint(0,255)
      b=randint(0,255)
      turtle.colormode(255)
      turtle.pencolor(r,g,b)
      turtle.bk(x)
      turtle.rt(90)
      turtle.bk(x)
      turtle.rt(90)
      turtle.bk(x)
      turtle.rt(90)
      turtle.bk(x)
      turtle.rt(90)
      turtle.up()
      turtle.rt(9)
      turtle.down()
      turtle.update()
turtle.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