Python

Moderators: None (Apply to moderate this forum)
Number of threads: 400
Number of posts: 1055

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Python Inheritence Help Posted by smerny on 1 Dec 2009 at 1:13 PM
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.rect.setFill('lightgray')
        self.rect.draw(win)
        self.label = Text(center, label)
        self.label.draw(win)
        self.deactivate()

    def clicked(self, p):
        "Returns true if button active and p is inside"
        return self.active 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.label.setFill('black')
        self.rect.setWidth(2)
        self.active = True

    def deactivate(self):
        "Sets this button to 'inactive'."
        self.label.setFill('darkgrey')
        self.rect.setWidth(1)
        self.active = 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):
		        self.rect.setFill('black')
			self.label.setFill('green')

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__
self.__createButtons()
File "calc.pyw", line 37, in __createButtons
self.buttons.append(CustomButton(self.win,Point(cx,cy),.75,.75,label))
File "calc.pyw", line 11, in __init__
self.rect.setFill('black')
AttributeError: CustomButton instance has no attribute 'rect'

Do I need to rewrite the entire __init__ to change this?
Report
Re: Python Inheritence Help Posted by smerny on 1 Dec 2009 at 6:17 PM
actually came up with this:

class CustomButton(Button):
	def changeColors(self,label,bg,outline):
		self.label.setFill(label)
		self.rect.setFill(bg)
		self.rect.setOutline(outline)


and then just change it after making the button objects



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.