简体   繁体   English

在Python Zelle图形中创建Rectangle的子类

[英]Create a subclass of Rectangle in Python Zelle graphics

I'm trying to create a subclass of Rectangle in the Zelle graphics library in Python: 我正在尝试在Python的Zelle图形库中创建Rectangle的子类:

from graphics import*
win =GraphWin('tower of haoi',1000,600)
class DISC(Rectangle):
    def __init__(self,pt1,pt2,color):
        self.pt1=pt1
        self.pt2=pt2
        self.color=color
    def createdisc(self):
        self.setFill(self.color)
        self.draw(win)

disc1=DISC(Point(0,0),Point(28,10),color_rgb(230, 255, 245))
disc1.createdisc()
win.getMouse()
win.close()

But the created object doesn't accept any methods and gives the error: 但是创建的对象不接受任何方法并给出错误:

AttributeError: 'DISC' object has no attribute 'config'

The problem is that DISC.__init__() doesn't call the __init__() method of it's super class: 问题是DISC.__init__()不调用它的超类的__init__()方法:

from graphics import *

class DISC(Rectangle):
    def __init__(self, p1, p2, color):
        super().__init__(p1, p2)
        self.color = color

    def createdisc(self):
        self.setFill(self.color)
        self.draw(win)

win = GraphWin('Tower of Hanoi', 1000, 600)

disc1 = DISC(Point(0, 0), Point(28, 10), color_rgb(230, 255, 245))
disc1.createdisc()

win.getMouse()
win.close()

You don't need to store the two points in your object as you're already storing them because your object is a Rectangle . 由于对象是一个Rectangle因此您不需要将两个点存储在对象中,因为您已经存储它们。

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

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