OOP Workshop

Rectangle Button

Child Class of Button

class RectangleButton(Button):
    def __init__(self, buttonColor, buttonX, buttonY, width, height):
        super().__init__("Rectangle", buttonColor, buttonX, buttonY, width, height)
    
    def drawRectangle(self, aTurtle, width, height):
        aTurtle.setheading(0)
        aTurtle.forward(width)
        aTurtle.left(90)
        aTurtle.forward(height)
        aTurtlex.left(90)
        aTurtle.forward(width)
        aTurtle.left(90)
        aTurtle.forward(height)

    
    def drawButtonShape(self, aTurtle):
        aTurtle.pensize(2)
        self.drawRectangle(aTurtle, self.width, self.height)

    def drawShape(self, x, y, aTurtle):
        self.drawRectangle(aTurtle, x-aTurtle.xcor(), y - aTurtle.ycor())

Rectangle class extends the functionality of Button class by inheriting the properties and methods.

A child class may override the properties and methods of the base class, and add new properties or methods if necessary.

Adding a rectangle button

buttonTurtle = turtle.Turtle()
buttonTurtle.speed(20)
selectedButton = Button("Line","Orange", -130, 220, 40, 20)
shapeButtons = []
shapeButtons.append(selectedButton)
shapeButtons.append(RectangleButton("Green", -70, 200, 100, 40))