简体   繁体   English

Turtle 只完成循环的一次迭代并在 done() 处停止

[英]Turtle only completes one iteration of a loop and stops at done()

I am trying Python turtle and I am facing this error:我正在尝试 Python 乌龟,我遇到了这个错误:

pencolor(col) File"<string>",line 5,in pencolor turtle.Terminator

Here is my code:这是我的代码:

from turtle import*
import colorsys

speed(0)
pensize(3)
bgcolor('black')
hue=0.0

for i in range(300):
    col=colorsys.hsv_to_rgb(hue,1,1)
    pencolor(col)
    hue+=0.005
    circle(5-i,100)
    lt(80)
    circle(5-i,100)
    rt(100)
    done()

The problem is callingdone() in the loop.问题是在循环中调用done() Let's check the docs for this function:让我们检查一下这个 function 的文档:

turtle.done() Starts event loop - calling Tkinter's mainloop function. turtle.done()启动事件循环 - 调用 Tkinter 的mainloop Must be the last statement in a turtle graphics program.必须是海龟图形程序中的最后一条语句。 Must not be used if a script is run from within IDLE in -n mode (No subprocess) - for interactive use of turtle graphics.如果脚本在 -n 模式(无子进程)中从 IDLE 中运行,则不得使用 - 用于海龟图形的交互式使用。

There's no real exceptional error in your code per se, only a logical one related to misunderstanding done .您的代码本身没有真正的异常错误,只有与 misunderstanding done相关的逻辑错误。 It only runs through the first iteration of the loop before blocking on done forever.它只运行循环的第一次迭代,然后永远阻塞done When you terminate the program, turtle is still in the midst of its loop and doesn't exit cleanly, so whatever routine it's in at the time looks like it caused the error.当您终止程序时,turtle 仍处于其循环中间并且不会干净地退出,因此无论它当时处于什么例程中,它看起来都会导致错误。

The solution is to move done outside of the loop:解决方案是在循环之外done

from turtle import *
import colorsys

speed(0)
pensize(3)
bgcolor("black")
hue = 0.0

for i in range(300):
    col = colorsys.hsv_to_rgb(hue, 1, 1)
    pencolor(col)
    hue += 0.005
    circle(5 - i, 100)
    lt(80)
    circle(5 - i, 100)
    rt(100)

done()  # <--

This is one of many gotchas that makes turtle a bit less user-friendly for beginners than you'd think.这是许多让海龟对初学者来说比你想象的更不友好的陷阱之一。

In the interest of avoiding other gotchas, I would avoid the * wildcard import.为了避免其他陷阱,我会避免*通配符导入。 This dumps dozens of turtle functions into your script's namespace which can clash with your custom-defined functions, leading to hours of debugging agony.这会将数十个海龟函数转储到脚本的命名空间中,这些函数可能与您自定义的函数发生冲突,从而导致数小时的调试痛苦。 Best practice is to use only the import turtle statement at the top of your file.最佳做法是仅使用文件顶部的import turtle语句。

Better still is to always instantiate your turtles, t = turtle.Turtle() , and draw with t henceforth, even if it's only a single turtle.更好的是始终实例化你的海龟, t = turtle.Turtle() ,然后用t绘制,即使它只是一只海龟。

Finally, prefer using the long form of turtle commands unless you have difficulty typing.最后,除非你打字有困难,否则更喜欢使用长形式的海龟命令。 right and penup is much more comprehensible than rt and pu . rightpenuprtpu更容易理解。

I also formatted your code with Black , which I recommend doing before posting.我还使用Black格式化了您的代码,我建议在发布之前这样做。

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

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