Text
pykraken.Text A text object for rendering text to the active renderer.
More about Text
This class handles the rendered text instance. You must provide a Font object when creating a Text instance.
Note
A window/renderer must be created before using text. Typically you should call kn.window.create(...) first, which initializes the text engine.
At a glance
12 membersReference
PYTHON APIConstructor
methodCreate a Text object. The font object must remain in-scope for the lifetime of the Text.
Args
- font (
Font) — The font to use for rendering this text. - text (
str) — The initial text string (optional, defaults to empty).
Raises
- RuntimeError — If text creation fails.
color
propertycolor: ColorGet or set the color of the rendered text.
height
propertyheight: intGet the height in pixels of the current text.
Returns
- int — The text height.
shadow_color
propertyshadow_color: ColorGet or set the shadow color for the text.
shadow_offset
propertyshadow_offset: Vec2Get or set the shadow offset for the text.
size
propertysize: Vec2Get the size (width, height) of the current text as a Vec2.
Returns
- Vec2 — The text dimensions.
text
propertytext: strGet or set the text string to be rendered.
width
propertywidth: intGet the width in pixels of the current text.
Returns
- int — The text width.
wrap_width
propertywrap_width: intGet or set the wrap width in pixels for text wrapping.
Set to 0 to disable wrapping. Negative values are clamped to 0.
draw
methodDraw the text to the renderer at the specified position with alignment. A shadow is drawn if shadow_color.a > 0 and shadow_offset is not (0, 0).
Args
- pos (
Vec2 | None) — The position in pixels. Defaults to (0, 0). - anchor (
Vec2 | None) — The anchor point for alignment (0.0-1.0). Defaults to top left (0, 0).
Raises
- RuntimeError — If the renderer is not initialized or text drawing fails.
- RuntimeError — If the text font is not set or has gone out of scope.
get_rect
methodget_rect(self) -> RectGet the bounding rectangle of the current text.
Returns
- Rect — A rectangle with x=0, y=0, and width/height of the text.
set_font
methodset_font(self, font: Font) -> NoneSet the font to use for rendering this text.
Args
- font (
Font) — The font to use.