加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Turtle模块没有属性颜色?

发布时间:2020-12-20 12:28:41 所属栏目:Python 来源:网络整理
导读:当我尝试从 Python documentation on turtle 运行第一段示例代码时: from turtle import *color('red','yellow')begin_fill()while True: forward(200) left(170) if abs(pos()) 1: breakend_fill()done() 我得到一个NameError: NameError: name ‘color’
当我尝试从 Python documentation on turtle运行第一段示例代码时:

from turtle import *
color('red','yellow')
begin_fill()
while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()

我得到一个NameError:

NameError: name ‘color’ is not defined

调整导入并手动指定模块也不起作用:

import turtle
turtle.color('red','yellow')
turtle.begin_fill()
while True:
    turtle.forward(200)
    turtle.left(170)
    if abs(turtle.pos()) < 1:
        break
turtle.end_fill()
turtle.done()

我使用的是Python v3.2.3,根据文档显然包含turtle.color. Python也支持tkinter,因为import tkinter也可以.

完整的痕迹是:

Traceback (most recent call last):
  File "<path name that contains no spaces>/turtle.py",line 1,in <module>
    from turtle import *
  File "<path name that contains no spaces>turtle.py",line 2,in <module>
    color('red','yellow')
NameError: name 'color' is not defined

有多奇怪如果我输入shell,无论是命令行还是IDLE,并一次输入一个命令:

>>> from turtle import *
>>> color('red','yellow')

没有问题.只有当我在IDLE中打开一个新窗口时,输入所有命令,然后运行脚本.

解决方法

您将文件命名为“turtle.py”,因此在导入乌龟时,您将导入自己的文件而不是stdlib模块.更改程序的名称,并删除该目录中的所有.pyc文件.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读