10

我想在 Python中使用turtle模块。但是当我导入turtle模块时,出现以下错误:

$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle
Traceback (most recent call last):                                                                    
  File "<stdin>", line 1, in <module>                                                                          
  File "turtle.py", line 3, in <module>                                                                                 
    myTurtle = turtle.Turtle()                                                                                                 
AttributeError: 'module' object has no attribute 'Turtle'

对于 Python 3.x:

$ python3
Python 3.2.3 (default, Sep 30 2012, 16:41:36) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "turtle.py", line 3, in <module>
    myTurtle = turtle.Turtle()
AttributeError: 'module' object has no attribute 'Turtle'

我在 Kubuntu Linux 12.10 下工作。我玩过 Tkinter gui。没有问题。乌龟模块会发生什么?

4

7 回答 7

29

您已经调用了一个脚本,它在标准库turtle.py中隐藏了模块。turtle重命名它。

于 2013-07-08T15:14:48.550 回答
3

python-tk您可以通过安装软件包来解决此问题。

sudo apt-get install python3-tk
于 2016-03-12T21:44:36.487 回答
0

还要确保您运行:

which python 然后用它设置文件的顶部。

#!/usr/bin/python
import Tkinter
from turtle import *

或者如果你没有正确的位置,你需要用 .py 重命名文件然后用 python filename.py 执行

于 2019-09-18T01:58:13.100 回答
0

在安装之前尝试使用:

from turtle import *

g=Turtle()

begin_fill()

g.forward(100)

检查这是否有效(导入和星号之间有空格)

于 2019-08-17T06:36:59.597 回答
0

我遇到了同样的问题,但我找到了答案:“重命名它!” 它奏效了。无论如何不要使用“进口龟”。使用“从龟进口 *”。

于 2016-06-20T13:36:28.013 回答
0

我无法找到任何文件,turtle.py因此我卸载了 Python 并从 Python.org 重新安装了 64 位版本。这次程序在我在终端中输入以下两行代码(黑屏)后运行。

import turtle

shelly=turtle.Turtle()

当然你的乌龟也可以叫别的名字,不一定shelly

于 2019-04-23T14:52:12.580 回答
-3

请试试

from tutle imort Turtle:导入模块....它将为您工作

于 2015-07-31T13:23:08.103 回答