CLASS v1.8.0

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.

Reference

PYTHON API
Text(font: Font, text: str = '')

Create 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

property
color: Color

Get or set the color of the rendered text.

height

property
height: int

Get the height in pixels of the current text.

Returns

  • int — The text height.

shadow_color

property
shadow_color: Color

Get or set the shadow color for the text.

shadow_offset: Vec2

Get or set the shadow offset for the text.

size

property
size: Vec2

Get the size (width, height) of the current text as a Vec2.

Returns

  • Vec2 — The text dimensions.

text

property
text: str

Get or set the text string to be rendered.

width

property
width: int

Get the width in pixels of the current text.

Returns

  • int — The text width.

wrap_width

property
wrap_width: int

Get or set the wrap width in pixels for text wrapping.

Set to 0 to disable wrapping. Negative values are clamped to 0.

draw

method
draw(self, pos: Vec2 = Vec2(0.000000, 0.000000), anchor: Vec2 = Vec2(0.000000, 0.000000)) -> None

Draw 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

method
get_rect(self) -> Rect

Get the bounding rectangle of the current text.

Returns

  • Rect — A rectangle with x=0, y=0, and width/height of the text.

set_font

method
set_font(self, font: Font) -> None

Set the font to use for rendering this text.

Args

  • font (Font) — The font to use.

Find your next step

Start typing to search the manual.