首页 > 解决方案 > 找不到海龟命令名称

问题描述

我无法让函数调用正常工作。这是一个示例调用:

def polyline(t,n,length,angle):
    """Draws n line segments with the given length and 
    angle(in degrees) between them. t is a turtle.
    """
    for i in range(n):
        t.fd(length)
        t.lt(angle)

相关的调用就像

alex=turtle.Turtle()

polyline(alex,5,100,90)

我已经导入了海龟,但出现以下错误:

TclError: invalid command name ".!canvas"

我错过了什么?

标签: pythonturtle-graphics

解决方案


看来我必须在进行函数调用之前不断定义 alex。例如这有效:

 def polyline(t,n,length,angle):
    """Draws n line segments with the given length and 
    angle(in degrees) between them. t is a turtle.
    """
    for i in range(n):
        t.fd(length)
        t.lt(angle)


alex=turtle.Turtle() 
#Test polyline
polyline(alex,5,780,90)

这失败了:

alex=turtle.Turtle() 
#insert some other functions


 #define polyline function

#call polyline

推荐阅读