简体   繁体   English

attributererror: 部分初始化的模块“turtle”没有属性“bgcolor”

[英]attributeerror: partially initialized module 'turtle' has no attribute 'bgcolor'

When I use turtle no matter what I get this error message:当我使用 turtle 时,无论我收到什么错误消息:

File "[REDACTED FOR SECURITY]", line 1, in <module>
    from turtle import *
  File "[REDACTED FOR SECURITY]", line 5, in <module>
    turtle.bgcolor('black')
AttributeError: partially initialized module 'turtle' has no attribute 'bgcolor' (most likely due to a circular import)

I wasn't even using turtle.bgcolor() .我什至没有使用turtle.bgcolor()

Here is my code, to draw a fern:这是我的代码,用于绘制蕨类植物:

from turtle import *
import random

pen = turtle.Turtle()
pen.speed(15)
pen.color("blue")
pen.penup()

x = 0
y = 0
for n in range(110000):
    pen.goto(65 * x, 37 * y - 252)  # 57 is to scale the fern and -275 is to start the drawing from the bottom.
    pen.pendown()
    pen.dot()
    pen.penup()
    r = random.random()  # to get probability
    r = r * 100
    xn = x
    yn = y
    if r < 1:  # elif ladder based on the probability
        x = 0
        y = 0.16 * yn
    elif r < 86:
        x = 0.85 * xn + 0.04 * yn
        y = -0.04 * xn + 0.85 * yn + 1.6
    elif r < 93:
        x = 0.20 * xn - 0.26 * yn
        y = 0.23 * xn + 0.22 * yn + 1.6
    else:
        x = -0.15 * xn + 0.28 * yn
        y = 0.26 * xn + 0.24 * yn + 0.44

As pointed out in this answer, you need to import the turtle module itself, instead of only importing all of its attributes:正如这个答案中所指出的,您需要导入turtle模块本身,而不是只导入它的所有属性:

import turtle

Thee reason is because you have你的原因是因为你有

pen = turtle.Turtle()

where if you didn't do import turtle , the turtle in that line wouldn't be defined.如果您不执行import turtle ,则不会定义该行中的turtle


One thing I want to address is that you don't need the turtle pen to be down in order for the turtle.dot() method to work, hence reducing the我想解决的一件事是,您不需要放下乌龟笔来让turtle.dot()方法工作,从而减少

    pen.pendown()
    pen.dot()
    pen.penup()

to

    pen.dot()

will effectively improve your program's efficiency.将有效提高你程序的效率。

The comment by @furas about your import statement is on the mark (+1). @furas 关于您的import语句的评论在标记 (+1) 上。 Based on other calls in your code, eg speed(15) and pendown() before dot() , it's clear you've not worked with turtle before and/or read its documentation.根据代码中的其他调用,例如speed(15)pendown() before dot() ,很明显您之前没有使用过 turtle 和/或阅读过它的文档。

If I were writing this for pure speed, I'd do something more like:如果我是为了纯粹的速度而写这篇文章,我会做更像的事情:

from turtle import Screen, Turtle
from random import random

x, y = 0, 0

def draw():
    global x, y

    turtle.goto(65 * x, 37 * y - 252)
    turtle.dot()

    r = random() * 100  # to get probability

    xn, yn = x, y

    if r < 1:  # elif ladder based on the probability
        x = 0
        y = 0.16 * yn
    elif r < 86:
        x = 0.85 * xn + 0.04 * yn
        y = -0.04 * xn + 0.85 * yn + 1.6
    elif r < 93:
        x = 0.20 * xn - 0.26 * yn
        y = 0.23 * xn + 0.22 * yn + 1.6
    else:
        x = -0.15 * xn + 0.28 * yn
        y = 0.26 * xn + 0.24 * yn + 0.44

    screen.update()
    screen.ontimer(draw)

screen = Screen()
screen.tracer(False)

turtle = Turtle()
turtle.color('blue')
turtle.penup()

draw()

screen.exitonclick()

This will run forever until you click on the window, whereupon it will stop and disappear.这将永远运行,直到您单击 window,然后它将停止并消失。

在此处输入图像描述

暂无
暂无

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

相关问题 AttributeError: 部分初始化的模块 &#39;turtle&#39; 没有属性 &#39;Turtle&#39;(很可能是由于循环导入) - AttributeError: partially initialized module 'turtle' has no attribute 'Turtle' (most likely due to a circular import) AttributeError:部分初始化的模块“turtle”没有属性“Pen”(很可能是由于循环导入) - AttributeError: partially initialized module 'turtle' has no attribute 'Pen' (most likely due to a circular import) AttributeError:部分初始化的模块“pandas”没有属性“DataFrame” - AttributeError: partially initialized module 'pandas' has no attribute 'DataFrame' Python/Json AttributeError:部分初始化的模块“json”没有属性 - Python/Json AttributeError: partially initialized module 'json' has no attribute 不断收到这个 AttributeError:部分初始化的模块 'schedule' 没有属性 'every' - Keep getting this AttributeError: partially initialized module 'schedule' has no attribute 'every' AttributeError: 模块“turtle”没有属性“turtle” - AttributeError: module 'turtle' has no attribute 'turtle' AttributeError: 模块 &#39;turtle&#39; 在 spyder 中没有属性 &#39;screen&#39;? - AttributeError: module 'turtle' has no attribute 'screen' in spyder? AttributeError:部分初始化的模块“cv2”没有属性“CascadeClassifier”(很可能是由于循环导入) - AttributeError: partially initialized module 'cv2' has no attribute 'CascadeClassifier' (most likely due to a circular import) AttributeError: 部分初始化的模块“tensorflow”没有属性“config”(很可能是由于循环导入) - AttributeError: partially initialized module 'tensorflow' has no attribute 'config' (most likely due to a circular import) AttributeError:部分初始化的模块“face_recognition”没有属性“face_encodings” - AttributeError: partially initialized module 'face_recognition' has no attribute 'face_encodings'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM