Creating color buttons. This is an exercise on the object-oriented concepts of encapsulation, inheritance, and polymorphism. You are given a Button class that is able to create Button objects large enough to contain their labels. Each Button can be enabled, disabled, and knows that it is clicked if it is enabled and a user uses the mouse to click it. Your job is to design, implement, and test a ColorButton class that extends the Button class as described below. Recall the checking account extension that you did in class when thinking about this problem.
The following was changed, 10/10/07:
After calling the superclass's constructor (as the SavingsAccount constructor did in bankAccount.py), the constructor of ColorButton should initialize appropriately named instance variables to store these colors.
Added 10/10/07: The constructor of ColorButton must call the superclass constructor to finish initialization. Because Button's constructor calls other methods, you'll need to initialize ColorButton's new instance fields before calling the constructor. So your ColorButton constructor should look like this:
def __init__(self, win, label, center, listOfColors):
# TODO: initialize additional instance fields of ColorButton
Button.__init__(self, win, label, center)
Now add new versions of the enable( ), and disable( ) methods that appropriately change the color of a ColorButton object when it is enabled or disabled. Use the same signature for these methods as those used in the Button class.
Add code to the colorButtonTest() function to test the enable() and disable() methods. The code that you add can be similar to the code in the buttonTest() function.
Run your program and notice the behavior. When the color buttons are enabled or disabled the colors should change according to the description in part d. above. Do you see why?
Added 10/10/07: You'll probably also notice that the label of the button is now hidden behind the rectangle. This is due to a "bug" in our implementation of Button. When we designed button we didn't anticipate adding a subclass that would color the rectangle. We assumed the rectangle would remain unfilled, so we could safely draw the button text first, then draw the rectangle on top. To correct this problem please swap lines 38 and 39 of buttonWidget.py. This is the only change you should make to that file.
Once you are certain that your implementation works, commit your work to your repository.
Project work: Try to make some progress on your Quixo team project. Take note of your milestone for session 20. By then your program should be able to show the game state for any given state of the game.
Polymorphism: The object-oriented concept known as polymorphism is not unique to Python. It's use is prevalent in most programming languages that support object-oriented development. Here is a description of the concept of polymorphism; note that the first example is written in Python!