简体   繁体   English

为什么当它的格式正确时会出现属性错误?

[英]Why am i getting a attribute error when its in the right format?

I keep getting a issue where color bassed commands arent working.我不断遇到基于颜色的命令不起作用的问题。 I have tried variables and (_color) in front of the 'color'.我在“颜色”前面尝试了变量和(_color)。 I have never ran into this problem befor.我以前从未遇到过这个问题。

import turtle

wn = turtle.Screen()
wn.setup(400,400)

ray = turtle.Turtle
ray.pencolor('red')
ray.dot(20)
Traceback (most recent call last):
  File "/home/pi/mu_code/words(1).py", line 7, in <module>
    ray.pencolor('red')
  File "/usr/lib/python3.7/turtle.py", line 2257, in pencolor
    return self._color(self._pencolor)
AttributeError: 'str' object has no attribute '_color'

You forgot to put the parentheses, because of which instead of object of Turtle class, ray is being misassigned.你忘了放括号,因为不是Turtle class 的 object, ray被错误分配。

>>> import turtle
>>> ray = turtle.Turtle
>>> type(ray)
<class 'type'>
>>> ray = turtle.Turtle()
>>> type(ray)
<class 'turtle.Turtle'>

Edit the below line in your code.在您的代码中编辑以下行。

ray = turtle.Turtle()

will work.将工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM