简体   繁体   English

Python乌龟模块.ontimer循环问题

[英]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. 当我使用for循环时,该功能按预期工作。 when I try to use the while loop, nothing happens, no runtime error either: 当我尝试使用while循环时,什么也没有发生,也没有运行时错误:

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: 我想说您的for循环和while循环在启动计时器方面都存在问题,我希望能像这样进行更多的交接:

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. 但是您的问题中没有足够的代码来理解全局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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