Python 将 Turtle Canvas 起来有问题

Python Having Problems Getting Turtle's Canvas Up

我刚在圣诞节收到一本关于 Python 儿童编码的书。我真的很高兴能使用它并立即下载了该应用程序。书上说用这段代码导入Turtle:

>>> import turtle

然后它说这是创建一个canvas:

>>> t = turtle.Pen ()

出现此错误消息。

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    t = turtle.Pen ()
AttributeError: module 'turtle' has no attribute 'Pen'

这是什么意思?如何让 Turtle 的 canvas 起来?

使用

# load the turtle module
import turtle

# the imported module turtle has a Turtle() that moves around 
# give your turtle a name so you can refer to turtle.Turtle() by the shorter name
# tim (i.e.)
tim = turtle.Turtle()

# Move tim
tim.forward(50)

# end turtling
turtle.done()

tim ( turtle.Turtle() ) 可以做更多的事情,这个页面包含关于乌龟的信息,它的方法和一些例子:https://docs.python.org/3/library/turtle.html

import turtle后,要输入一个变量名,通过该变量调用turtle。

语法:

import turtle
your_variable_name = turtle.Turtle()

之后,你要接入海龟。

your_variable_name.pencolor('blue')

你所做的实际上是正确的,尽管有评论和其他答案:

>>> import turtle
>>> t = turtle.Pen ()

例如:

> python3
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle
>>> t = turtle.Pen()
>>> t
<turtle.Turtle object at 0x101ca4ac8>
>>> ^D

问题是,为什么它对 不起作用?我最初的猜测是您在工作目录中创建了一个名为 turtle.py 的文件——如果是这样,请重命名或删除它并重试。否则,当您 import turtle 时,Python 会找到您的 turtle.py 文件,而不是 Python 库 turtle.py 文件。如果不是这样:

我的第二个猜测是您正在使用一个有限的 Python 环境来实现它自己的 turtle 子集。例如,https://trinket.io/turtle 上的 turtle 在线编码使用这样一个子集,其中 Pen() 不起作用,但 Turtle()(它是标准 turtle 库中的别名)工作得很好。所以尝试 Turtle() 而不是 Pen()