pyglet.text

Submodules

Details

Text formatting, layout and display.

This module provides classes for loading styled documents from text files, HTML files and a pyglet-specific markup format. Documents can be styled with multiple fonts, colours, styles, text sizes, margins, paragraph alignments, and so on.

Using the layout classes, documents can be laid out on a single line or word-wrapped to fit a rectangle. A layout can then be efficiently drawn in a window or updated incrementally (for example, to support interactive text editing).

The label classes provide a simple interface for the common case where an application simply needs to display some text in a window.

A plain text label can be created with:

label = pyglet.text.Label('Hello, world',
                          font_name='Times New Roman',
                          font_size=36,
                          x=10, y=10)

Alternatively, a styled text label using HTML can be created with:

label = pyglet.text.HTMLLabel('<b>Hello</b>, <i>world</i>',
                              x=10, y=10)

Either label can then be drawn at any time with:

label.draw()

For details on the subset of HTML supported, see pyglet.text.formats.html.

Refer to the Programming Guide for advanced usage of the document and layout classes, including interactive editing, embedding objects within documents and creating scrollable layouts.

class Weight

An Enum of known cross-platform font weight strings.

Each value is both an Enum and a str. This is not a built-in Python StrEnum to ensure compatibility with Python < 3.11.

Important

Fonts will use the closest match if they lack a weight!

The values of this enum imitate the string names for font weights as used in CSS and the OpenType specification. Numerical font weights are not supported because:

  • Integer font weight support and behavior varies by back-end

  • Some font renderers do not support or round float values

  • Some font renderers lack support for variable-width fonts

Additional weight strings may be supported by certain font-rendering back-ends. To learn more, please see your platform’s API documentation and the following:

  1. The MDN article on CSS font weights

  2. The OpenType specification

THIN = 'thin'
EXTRALIGHT = 'extralight'
LIGHT = 'light'
NORMAL = 'normal'

The default weight for a font.

MEDIUM = 'medium'
SEMIBOLD = 'semibold'
BOLD = 'bold'

The default bold style for a font.

EXTRABOLD = 'extrabold'
ULTRABOLD = 'ultrabold'
__new__(value)
exception DocumentDecodeException

An error occurred decoding document text.

class DocumentDecoder

Abstract document decoder.

class DocumentLabel

Base label class.

A label is a layout that exposes convenience methods for manipulating the associated document.

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

Text color.

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

property font_name: str | list[str]

Font family name.

The font name, as passed to pyglet.font.load(). A list of names can optionally be given: the first matching font will be used.

property font_size: float

Font size, in points.

property italic: bool | str

Italic font style.

property opacity: int

Blend opacity.

This property sets the alpha component of the colour of the label’s vertices. With the default blend mode, this allows the layout to be drawn with fractional opacity, blending with the background.

An opacity of 255 (the default) has no effect. An opacity of 128 will make the label appear semi-translucent.

property text: str

The text of the label.

property weight: str

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

See the Weight enum for valid cross-platform string values.

class HTMLLabel

HTML formatted text label.

A subset of HTML 4.01 is supported. See pyglet.text.formats.html for details.

property text: str

HTML formatted text of the label.

class Label

Plain text label.