Python Inheritence Help - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Python Inheritence Help

smernysmerny Posts: 2Member
I have a simple button class to make buttons
from graphics import *

class Button:

"""A button is a labeled rectangle in a window.
It is activated or deactivated with the activate()
and deactivate() methods. The clicked(p) method
returns true if the button is active and p is inside it."""

def __init__(self, win, center, width, height, label):
""" Creates a rectangular button, eg:
qb = Button(myWin, Point(30,25), 20, 10, 'Quit') """

w,h = width/2.0, height/2.0
x,y = center.getX(), center.getY()
self.xmax, self.xmin = x+w, x-w
self.ymax, self.ymin = y+h, y-h
p1 = Point(self.xmin, self.ymin)
p2 = Point(self.xmax, self.ymax)
self.rect = Rectangle(p1,p2)
self.label = Text(center, label)

def clicked(self, p):
"Returns true if button active and p is inside"
return and
self.xmin <= p.getX() <= self.xmax and
self.ymin <= p.getY() <= self.ymax

def getLabel(self):
"Returns the label string of this button."
return self.label.getText()

def activate(self):
"Sets this button to 'active'."
self.rect.setWidth(2) = True

def deactivate(self):
"Sets this button to 'inactive'."
self.rect.setWidth(1) = False
and I wanted to make a CustomButton class which inherited Button, except with a black button and green text, I tried this
class CustomButton(Button):
def __init__(self, win, center, width, height, label):
But I get this error:

Traceback (most recent call last):
File "calc.pyw", line 104, in <module>
theCalc = Calculator()
File "calc.pyw", line 21, in __init__
File "calc.pyw", line 37, in __createButtons
File "calc.pyw", line 11, in __init__
AttributeError: CustomButton instance has no attribute 'rect'

Do I need to rewrite the entire __init__ to change this?


  • smernysmerny Posts: 2Member
    actually came up with this:

    [code]class CustomButton(Button):
    def changeColors(self,label,bg,outline):

    and then just change it after making the button objects
Sign In or Register to comment.