我正试图使用下面的代码使用Turtle,但当窗口弹出时,什么也没发生,screen.exitonclick((也不起作用。屏幕只闪烁了几秒钟就停止了。有什么想法吗?
from turtle import Turtle, Screen
timmy_the_turtle = Turtle()
timmy_the_turtle.shape("turtle")
timmy_the_turtle.color("red")
timmy_the_turtle.forward(100)
timmy_the_turtle.right(90)
screen = Screen()
screen.exitonclick()
然后我得到以下错误:
Traceback (most recent call last):
File "/Users/Work/PycharmProjects/Day18/main.py", line 5, in <module>
timmy_the_turtle.color("red")
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/turtle.py", line 2217, in color
pcolor = self._colorstr(pcolor)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/turtle.py", line 2697, in _colorstr
return self.screen._colorstr(args)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/turtle.py", line 1159, in _colorstr
raise TurtleGraphicsError("bad color string: %s" % str(color))
turtle.TurtleGraphicsError: bad color string: red
我会检查它是否在IDLE中工作。如果它有效,那么它就是PyCharm问题。请尝试重新安装。如果不起作用,请尝试在乌龟移动后添加一个乌龟.done(((不是乌龟的名称,只是模块乌龟(。
我遇到了完全相同的问题。我发现我的PyCharm解释器被设置为使用Python 2.7。我把它改为使用Python 3.10,这就解决了问题。