Text Class

Inheritance diagram of Text

class Text(font, text='', x=0, y=0, z=0, color=(1, 1, 1, 1), width=None, halign='left', valign='baseline')

Simple displayable text.

This is a convenience class for rendering strings of text. It takes care of caching the vertices so the text can be rendered every frame with little performance penalty.

Text can be word-wrapped by specifying a width to wrap into. If the width is not specified, it gives the width of the text as laid out.

Variables:
  • x – X coordinate of the text
  • y – Y coordinate of the text

Warning

Deprecated. Use pyglet.text.Label.

Constructor:

__init__(font, text='', x=0, y=0, z=0, color=(1, 1, 1, 1), width=None, halign='left', valign='baseline')

Create displayable text.

Parameters:
  • font (Font) – Font to render the text in.
  • text (str) – Initial string to render.
  • x (float) – X coordinate of the left edge of the text.
  • y (float) – Y coordinate of the baseline of the text. If the text is word-wrapped, this refers to the first line of text.
  • z (float) – Z coordinate of the text plane.
  • color (4-tuple of float) – Color to render the text in. Alpha values can be specified in the fourth component.
  • width (float) – Width to limit the rendering to. Text will be word-wrapped if necessary.
  • halign (str) – Alignment of the text. See Text.halign for details.
  • valign (str) – Controls positioning of the text based off the y coordinate. One of BASELINE, BOTTOM, CENTER or TOP. Defaults to BASELINE.

Methods:

draw()

Attributes:

BASELINE Align the baseline of the first line of text with the given Y coordinate.
BOTTOM Align the bottom of the descender of the final line of text with the given Y coordinate.
CENTER Align the horizontal center of the text to the given X coordinate.
LEFT Align the left edge of the text to the given X coordinate.
RIGHT Align the right edge of the text to the given X coordinate.
TOP Align the top of the ascender of the first line of text with the given Y coordinate.
color
font
halign Horizontal alignment of the text.
height Height of the text.
leading Vertical space between adjacent lines, in pixels.
line_height Vertical distance between adjacent baselines, in pixels.
text Text to render.
valign Vertical alignment of the text.
width Width of the text.
x
y
z

Methods

Text.draw()

Attributes

Text.BASELINE = 'baseline'

Align the baseline of the first line of text with the given Y coordinate.

Text.BOTTOM = 'bottom'

Align the bottom of the descender of the final line of text with the given Y coordinate.

Text.CENTER = 'center'

Align the horizontal center of the text to the given X coordinate.

Text.LEFT = 'left'

Align the left edge of the text to the given X coordinate.

Text.RIGHT = 'right'

Align the right edge of the text to the given X coordinate.

Text.TOP = 'top'

Align the top of the ascender of the first line of text with the given Y coordinate.

Text.color
Text.font
Text.halign

Horizontal alignment of the text.

The text is positioned relative to x and width according to this property, which must be one of the alignment constants LEFT, CENTER or RIGHT.

Type:str
Text.height

Height of the text.

This property is the ascent minus the descent of the font, unless there is more than one line of word-wrapped text, in which case the height takes into account the line leading. Read-only.

Type:float
Text.leading

Vertical space between adjacent lines, in pixels.

Type:int
Text.line_height

Vertical distance between adjacent baselines, in pixels.

Type:int
Text.text

Text to render.

The glyph vertices are only recalculated as needed, so multiple changes to the text can be performed with no performance penalty.

Type:str
Text.valign

Vertical alignment of the text.

The text is positioned relative to y according to this property, which must be one of the alignment constants BOTTOM, BASELINE, CENTER or TOP.

Type:str
Text.width

Width of the text.

When set, this enables word-wrapping to the specified width. Otherwise, the width of the text as it will be rendered can be determined.

Type:float
Text.x
Text.y
Text.z

Table Of Contents

Previous topic

GlyphString Class

Next topic

add_directory Function