PyCharm:在'turtle.py'中找不到对'xxx'的引用。

3

我尝试使用内置的turtle库画一个五角星。

环境:

  • Windows 10
  • Python 3.5
  • PyCharm Community Edition 2017.1.5

代码:

import turtle


turtle.fillcolor('red')
turtle.begin_fill()
while True:
    turtle.forward(200)
    turtle.right(144)
    if abs(turtle.pos()) < 1:
        break
turtle.end_fill()

所有的海龟库中的方法,包括'fillcolor'、'begin_fill'、'forward'、'right'和'pos'等,在PyCharm中都会出现"在'turtle.py'中找不到参考引用 'xxx'" 的警告,并且这些方法的自动完成也会随着警告而失败。但是奇怪的是,脚本可以正常运行并按预期正确地执行。
我已经在SO上搜寻了答案,有一些相关的问题,但实际上不同:
1. 在'__init__.py'中找不到参考引用'xxx' 2. 未解决的引用'print'
以上所有问题的答案都无法解决此问题。
根据第一批评论和答案,以下是提供的一些更多信息:
- 我几乎相信我正在使用Python 3.x的turtle,因为在我的笔记本电脑上只有一个turtle.py文件,位于'C:\Python35\Lib'目录下。顺便说一下,如果我仍然可能使用Python 2.x的turtle,我如何检查此信息,并如何更新默认目录中的这个内置库? - 我几乎相信我没有使用虚拟环境,我的proj解释器是Python 3.5.2

你正在使用Python3编写代码,但是却在使用Python2的turtle。请确保你已经安装了适用于Python3的turtle库。 - inspectorG4dget
我几乎相信我正在使用Python3的turtle,因为在我的PC上只有一个'turtle.py'文件位于目录'C:\Python35\Lib'下。我将更新我的问题描述。 - LT. Zh
1个回答

2
问题出在\Lib\turtle.py文件中。
魔法变量all的定义如下:
__all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +
           _tg_utilities + ['Terminator']) # + _math_functions)

PyCharm在扫描包时不会执行此代码。因此,PyCharm无法定义可以在模块级别使用的函数。

因此,您可以更改turtle.py:

# __all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +
#            _tg_utilities + ['Terminator']) # + _math_functions)
__all__ = ['ScrolledCanvas', 'TurtleScreen', 'Screen', 'RawTurtle', 'Turtle', 'RawPen', 'Pen', 'Shape', 'Vec2D', 'back',
           'backward', 'begin_fill', 'begin_poly', 'bk', 'addshape', 'bgcolor', 'bgpic', 'bye', 'clearscreen',
           'colormode', 'delay', 'exitonclick', 'getcanvas', 'getshapes', 'listen', 'mainloop', 'mode', 'numinput',
           'onkey', 'onkeypress', 'onkeyrelease', 'onscreenclick', 'ontimer', 'register_shape', 'resetscreen',
           'screensize', 'setup', 'Terminator', 'setworldcoordinates', 'textinput', 'title', 'tracer', 'turtles',
           'update', 'window_height', 'window_width', 'write_docstringdict', 'done', 'circle', 'clear', 'clearstamp',
           'clearstamps', 'clone', 'color', 'degrees', 'distance', 'dot', 'down', 'end_fill', 'end_poly', 'fd',
           'fillcolor', 'filling', 'forward', 'get_poly', 'getpen', 'getscreen', 'get_shapepoly', 'getturtle', 'goto',
           'heading', 'hideturtle', 'home', 'ht', 'isdown', 'isvisible', 'left', 'lt', 'onclick', 'ondrag', 'onrelease',
           'pd', 'pen', 'pencolor', 'pendown', 'pensize', 'penup', 'pos', 'position', 'pu', 'radians', 'right', 'reset',
           'resizemode', 'rt', 'seth', 'setheading', 'setpos', 'setposition', 'settiltangle', 'setundobuffer', 'setx',
           'sety', 'shape', 'shapesize', 'shapetransform', 'shearfactor', 'showturtle', 'speed', 'st', 'stamp', 'tilt',
           'tiltangle', 'towards', 'turtlesize', 'undo', 'undobufferentries', 'up', 'width', 'write', 'xcor', 'ycor']

此外,您还可以使用 Turtle 类:
T = turtle.Turtle()
T.fillcolor('red')
etc

这个可以运行,但我的Turtle应用程序在PyCharm中运行时不响应键盘输入,就像在命令行中运行时那样。 - Casey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接