Create the Counters custom widget shown in Figure 11.8. The widget should show a 3 x 3 grid, with each square either blank (showing just the background color) or with a red or yellow ellipse. The state of any grid square should change from blank to red to yellow and back to blank in an endless cycle. Changes of state should occur when the user clicks a square or presses the spacebar on a square. The keyboard focus should be shown by drawing the square with a thick blue pen instead of the normal thin black pen used for the other squares. The user should be able to change the focused square by clicking a square or by using the up, down, left, and right arrow keys to move the focus.
Make sure that you provide a size hint and minimum size hint so that the widget has good resizing behavior and cannot be shrunk too small. The paint event is quite short, but slightly subtle; you will probably need to save and restore the painter's state, using QPainter.save() and QPainter.restore(), so that pen and brush colors intended for one square don't propagate to others.
Include an if _name_ == "_main_": statement at the end, and create a
QApplication object and an instance of the Counters widget so that you can test it. The whole thing can be done in less than 130 lines.
A solution is given in chap11/counters.py.
This page intentionally left blank
• Animation and Complex Shapes
Was this article helpful?
Discover How You Can Quickly And Easily Dominate Google and YouTube... With Simple Cash Generating Videos. Did you know that YouTube is the second largest search website on the entire Internet? YouTube gets more daily searches than Bing and Yahoo. In fact, there is only one search engine that gets more action.