psychos.visual.Text#

class psychos.visual.Text(text: str = '', position: Tuple[float, float] = (0, 0), width: int | None = None, height: int | None = None, color: ColorType | None = None, anchor_x: AnchorHorizontal = 'center', anchor_y: AnchorVertical = 'center', align: AnchorHorizontal = 'center', rotation: float = 0, multiline: bool = False, font_name: str | None = None, font_size: float | None = None, italic: bool = False, stretch: bool = False, window: Window | None = None, coordinates: UnitType | Unit | None = None, **kwargs)[source]#

Bases: Label

A class to represent text in a Pyglet window using a Label component.

Parameters:
  • text (str, default="") – The text to display.

  • position (Tuple[float, float], default=(0, 0)) – The position of the text in the window.

  • width (Optional[int], default=None) – The width of the text box.

  • height (Optional[int], default=None) – The height of the text box.

  • color (Optional[ColorType], default=None) – The color of the text.

  • anchor_x (AnchorHorizontal, default="center") – The horizontal anchor alignment of the text.

  • anchor_y (AnchorVertical, default="center") – The vertical anchor alignment of the text.

  • window (Optional[Window], default=None) – The window in which the text will be displayed.

  • rotation (float, default=0) – The rotation angle of the text.

  • multiline (bool, default=False) – Whether the text can span multiple lines.

  • font_name (Optional[str], default=None) – The name of the font to use.

  • font_size (Optional[float], default=None) – The size of the font to use.

  • italic (bool, default=False) – Whether the text is italicized.

  • stretch (bool, default=False) – Whether the text is stretched.

  • align (AnchorHorizontal, default="center") – The alignment of the text.

  • coordinate_units (Optional[Union[UnitType, Units]], default=None) – The unit system to be used for positioning the text.

  • kwargs (dict) – Additional keyword arguments to pass to the Pyglet Label.

__init__(text: str = '', position: Tuple[float, float] = (0, 0), width: int | None = None, height: int | None = None, color: ColorType | None = None, anchor_x: AnchorHorizontal = 'center', anchor_y: AnchorVertical = 'center', align: AnchorHorizontal = 'center', rotation: float = 0, multiline: bool = False, font_name: str | None = None, font_size: float | None = None, italic: bool = False, stretch: bool = False, window: Window | None = None, coordinates: UnitType | Unit | None = None, **kwargs)[source]#

Create a plain text label.

Parameters:
  • text – Text to display.

  • x – X coordinate of the label.

  • y – Y coordinate of the label.

  • z – Z coordinate of the label.

  • width – Width of the label in pixels, or None

  • height – Height of the label in pixels, or None

  • anchor_x – Anchor point of the X coordinate: one of "left", "center" or "right".

  • anchor_y – Anchor point of the Y coordinate: one of "bottom", "baseline", "center" or "top".

  • rotation – The amount to rotate the label in degrees. A positive amount will be a clockwise rotation, negative values will result in counter-clockwise rotation.

  • multiline – If True, the label will be word-wrapped and accept newline characters. You must also set the width of the label.

  • dpi – Resolution of the fonts in this layout. Defaults to 96.

  • font_name – Font family name(s). If more than one name is given, the first matching name is used.

  • font_size – Font size, in points.

  • weight – The ‘weight’ of the font (boldness). See the Weight enum for valid cross-platform weight names.

  • italic – Italic font style.

  • stretch – Stretch font style.

  • color – Font color as RGBA or RGB components, each within 0 <= component <= 255.

  • align – Horizontal alignment of text on a line, only applies if a width is supplied. One of "left", "center" or "right".

  • batch – Optional graphics batch to add the label to.

  • group – Optional graphics group to use.

  • program – Optional graphics shader to use. Will affect all glyphs.

Methods

__init__([text, position, width, height, ...])

Create a plain text label.

begin_update()

Indicate that a number of changes to the layout or document are about to occur.

delete()

Deletes all vertices and boxes associated with the layout.

draw()

Draw this text layout.

end_update()

Perform pending layout changes since begin_update.

get_as_texture([min_filter, mag_filter])

Utilizes a Framebuffer to draw the current layout into a texture.

get_line_count()

Get the number of lines in the text layout.

get_style(name)

Get a document style value by name.

on_delete_text(start, end)

Event handler for AbstractDocument.on_delete_text.

on_insert_text(start, text)

Event handler for AbstractDocument.on_insert_text.

on_style_text(start, end, attributes)

Event handler for AbstractDocument.on_style_text.

set_style(name, value)

Set a document style value by name over the whole document.

Attributes

anchor_x

Horizontal anchor alignment.

anchor_y

Vertical anchor alignment.

batch

The Batch that this Layout is assigned to.

bottom

The y-coordinate of the bottom side of the layout.

color

Text color.

content_height

The calculated height of the text in the layout.

content_valign

Vertical alignment of content within larger layout box.

content_width

Calculated width of the text in the layout.

coordinates

Get the coordinate system used for the text.

document

Document to display.

dpi

Get DPI used by this layout.

font_name

Font family name.

font_size

Font size, in points.

group

Get the Group specified by the user.

height

The defined maximum height of the layout in pixels, or None.

italic

Italic font style.

left

The x-coordinate of the left side of the layout.

multiline

Set if multiline layout is enabled.

opacity

Blend opacity.

position

Get the position of the text in pixels.

program

The ShaderProgram that is assigned to this Layout.

right

The x-coordinate of the right side of the layout.

rotation

Rotation of the layout in degrees.

text

The text of the label.

top

The y-coordinate of the top side of the layout.

visible

True if the layout will be visible when drawn.

weight

The font weight (boldness or thickness), as a string.

width

The defined maximum width of the layout in pixels, or None.

x

X coordinate of the layout.

y

Y coordinate of the layout.

z

Z coordinate of the layout.

group_cache

property color: tuple[int, int, int, int]#

Text color.

Color is a 4-tuple of RGBA components, each in range [0, 255].

property coordinates: Unit#

Get the coordinate system used for the text.

draw() Text[source]#

Draw this text layout.

Note

This method performs very badly if a batch was supplied to the constructor. If you add this layout to a batch, you should ideally use only the batch’s draw method.

Note

If this is not its own batch, InlineElements will not be drawn.

property height: int | None#

The defined maximum height of the layout in pixels, or None.

When height is not None, it affects the positioning of the text when anchor_y and content_valign are used.

property position: Tuple[float, float]#

Get the position of the text in pixels.

property width: int | None#

The defined maximum width of the layout in pixels, or None.

If multiline and wrap_lines is True, the width defines where the text will be wrapped. If multiline is False or wrap_lines is False, this property has no effect.