问题描述
所以我在 pycharm 中执行时遇到了乌龟不绘制图像的问题。
窗口出现并保持打开状态,但光标只是停留在那里。它没有根据需要执行绘制图像的指令。这是在 2 个单独的文件中,main.py 和 turtle.cfg 文件。
main.py 中有这段代码
import turtle
t = turtle.Turtle()
t.width(2)
t.pencolor("red")
def square(t,length):
for count in range(4):
t.forward(length)
t.left(90)
square()
turtle.done()
turtle.cfg 里面有这个
width = 300
height = 200
using_IDLE = True
colormode = 255
就是这样。它应该非常简单,但由于某种原因,当我打开它时,光标只是位于屏幕中央而无处可去。为了透明起见,这是直接从我的教科书中复制的作为学习示例,所以我认为它应该工作。
解决方法
函数 square
从未被调用,因此它从未运行,您可以尝试将 square(t,40)
(或其他数字)放在 turtle.done()
之前,如下所示:
import turtle
t = turtle.Turtle()
t.width(2)
t.pencolor("red")
def square(t,length):
for count in range(4):
t.forward(length)
t.left(90)
square(t,40)
turtle.done()