【问题标题】:I made Class Object inherit Class Turtle. Getting AttributeError我让 Class Object 继承 Class Turtle。获取属性错误
【发布时间】:2020-09-05 10:00:01
【问题描述】:

我制作了一个单独的 python 文件来创建对象类。比我将这个类导入另一个文件并制作这个 pad1 对象。

pad1 = Object("square", "blue", -350, 0)
pad1.shapesize(stretch_len=1,stretch_wid=5)

但我收到一条错误消息:“对象”对象没有属性“_outlinewidth”

对象类代码

import turtle
from turtle import Turtle


class Object(Turtle):
    def __init__(self, shape, color, x, y):
        self = Turtle()
        self.speed(0)
        self.shape(shape)
        self.color(color)
        self.penup()
        self.goto(x, y)

我该如何解决这个问题?我想使用单独的文件和使用类来解决这个问题,类似于我尝试过的方式

【问题讨论】:

标签: python class inheritance turtle-graphics attributeerror


【解决方案1】:

@Rabbid76 是正确的 (+1),下面是您的代码的修改,包含注释并调整了一些其他问题:

from turtle import Screen, Turtle

class Pad(Turtle):
    def __init__(self, shape, color, position):
        super().__init__(shape=shape, visible=False)

        self.speed('fastest')
        self.color(color)
        self.penup()
        self.setposition(position)
        self.showturtle()

pad1 = Pad('square', 'blue', (-350, 0))
pad1.shapesize(stretch_len=1, stretch_wid=5)

screen = Screen()
screen.exitonclick()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 2010-11-04
    • 1970-01-01
    相关资源
    最近更新 更多