textEntry widget provides an interactive text input field that captures keyboard input from the user.
Constructor
Parameters
The pygame surface to which the text entry will draw itself
The position of the text entry field on the surface
The width and height of the text entry field
The initial text in the field
RGB color tuple for the text
Padding around the text (currently unused in implementation)
RGB color tuple for the background
Properties
text
The current text content of the input field. Can be read or modified directly.clicked
Boolean indicating whether the field is currently active (clicked). WhenTrue, the field captures keyboard input.
rect
The pygame.Rect representing the text entry’s bounding box.Methods
draw()
Renders the text entry field and handles keyboard input when active.mouse_click_event(pos)
Handles mouse click events to activate/deactivate the field.The (x, y) position of the mouse click
Usage Example
From the main game:game.py
game.py
Keyboard Input Handling
The text entry captures various keyboard inputs when active:Single Characters
Regular alphanumeric keys are appended to the text (if there’s room):widgets.py
Special Keys
- Tab: Adds 4 spaces
- Space: Adds a space character
- Backspace: Removes the last character
widgets.py
Key Repeat Delay
The widget includes a delay mechanism to prevent overly rapid key repeats:widgets.py
The source code includes a comment that the delay time needs improvement: “can’t seem to find a decent delay time please fix”
Text Wrapping
Currently, the text entry does not wrap to multiple lines. Text stops being added when it reaches the width limit:widgets.py
Drawing Implementation
The field is drawn as a white rectangle with black text:widgets.py
Click Detection
Clicking inside the text entry toggles its active state:widgets.py
Related Widgets
- Button - Another interactive widget for user interaction
- MessageBoard - For displaying static text instead of capturing input