简体   繁体   中英

Python turtle module .ontimer loop question

def h1():
    tess.forward(70)
    tess.fillcolor("orange")
def h2():
    tess.forward(70)
    tess.fillcolor("red")

def h3():
    tess.back(140)
    tess.fillcolor("green")

timer = 1000

for _ in range(timer):
    wn.ontimer(h1,timer)
    wn.ontimer(h2, timer+1000)
    wn.ontimer(h3, timer+2000)
    timer +=3000

wn.listen()
wn.mainloop()

When I use the for loop the function works as intended. when I try to use the while loop, nothing happens, no runtime error either:

while True:
    wn.ontimer(h1,timer)
    wn.ontimer(h2, timer+1000)
    wn.ontimer(h3, timer+2000)
    timer +=3000

I would say both your for loop and while loop are questionable in the manner they launch timers, I would have expected more of a handoff like:

from turtle import Screen, Turtle

TIME = 1000  # in milliseconds

def h1():
    tess.forward(70)
    tess.fillcolor("orange")
    screen.ontimer(h2, TIME)

def h2():
    tess.forward(70)
    tess.fillcolor("red")
    screen.ontimer(h3, TIME)

def h3():
    tess.back(140)
    tess.fillcolor("green")
    screen.ontimer(h1, TIME)

screen = Screen()

tess = Turtle()

h1()

screen.mainloop()

But there's not sufficient code in your question to understand the big picture.

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