Available Widgets
Box
Rectangular container with optional borders and background colors
MessageBoard
Text display board for showing multi-line messages
Button
Interactive buttons with click events and toggle states
Images
Static or animated image display (like spinners)
Text Entry
Input field for capturing user text
Moving Objects
Animated rectangles, images, and circles with collision detection
Common Patterns
Surface Parameter
Most widgets receive asurface argument in the constructor. This is the pygame surface to which the widget will draw itself when its draw() method is called.
Position and Rectangles
Widgets use two main approaches for positioning:- pygame.Rect: Used by Box and MessageBoard for defining rectangular areas
- vec2d: Used by interactive widgets like Button and textEntry for simpler positioning
Colors
Colors can be specified as:pygame.Colorobjects:pygame.Color('yellow')orpygame.Color(50, 20, 0)- RGB tuples:
(255, 255, 255)
Drawing Widgets
All widgets implement adraw() method that must be called each frame:
Widget Lifecycle
- Initialization: Create widget with surface and parameters
- Event Handling: Process user input (for interactive widgets)
- Drawing: Call
draw()method each frame to render
Exception Classes
The widget system defines two custom exceptions:WidgetError
Base exception class for widget-related errors.LayoutError
Raised when widget layout constraints are violated (e.g., text too large for MessageBoard).Unless otherwise specified, all rectangles are
pygame.Rect instances, and all colors are pygame.Color instances.Next Steps
Box Widget
Learn about the basic rectangular container
MessageBoard
Display text messages to users