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.

New in version 1.1.

exception DocumentDecodeException

An error occurred decoding document text.

class DocumentDecoder

Abstract document decoder.

decode(text, location=None)

Decode document text.

Parameters:
  • text (str) – Text to decode
  • location (Location) – Location to use as base path for additional resources referenced within the document (for example, HTML images).
Return type:

AbstractDocument

class DocumentLabel(document=None, x=0, y=0, width=None, height=None, anchor_x='left', anchor_y='baseline', multiline=False, dpi=None, batch=None, group=None)

Base label class.

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

get_style(name)

Get a document style value by name.

If the document has more than one value of the named style, pyglet.text.document.STYLE_INDETERMINATE is returned.

Parameters:name (str) – Style name to query. See documentation for pyglet.text.layout for known style names.
Return type:object
set_style(name, value)

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

Parameters:
  • name (str) – Name of the style to set. See documentation for pyglet.text.layout for known style names.
  • value (object) – Value of the style.
bold

Bold font style.

Type:bool
color

Text color.

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

Type:(int, int, int, int)
font_name

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.

Type:str or list
font_size

Font size, in points.

Type:float
italic

Italic font style.

Type:bool
text

The text of the label.

Type:str
class HTMLLabel(text='', location=None, x=0, y=0, width=None, height=None, anchor_x='left', anchor_y='baseline', multiline=False, dpi=None, batch=None, group=None)

HTML formatted text label.

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

text

HTML formatted text of the label.

Type:str
class Label(text='', font_name=None, font_size=None, bold=False, italic=False, color=(255, 255, 255, 255), x=0, y=0, width=None, height=None, anchor_x='left', anchor_y='baseline', align='left', multiline=False, dpi=None, batch=None, group=None)

Plain text label.

decode_attributed(text)

Create a document directly from some attributed text.

See pyglet.text.formats.attributed for a description of attributed text.

Parameters:text (str) – Attributed text to decode.
Return type:FormattedDocument
decode_html(text, location=None)

Create a document directly from some HTML formatted text.

Parameters:
  • text (str) – HTML data to decode.
  • location (str) – Location giving the base path for additional resources referenced from the document (e.g., images).
Return type:

FormattedDocument

decode_text(text)

Create a document directly from some plain text.

Parameters:text (str) – Plain text to initialise the document with.
Return type:UnformattedDocument
get_decoder(filename, mimetype=None)

Get a document decoder for the given filename and MIME type.

If mimetype is omitted it is guessed from the filename extension.

The following MIME types are supported:

text/plain
Plain text
text/html
HTML 4 Transitional
text/vnd.pyglet-attributed
Attributed text; see pyglet.text.formats.attributed

DocumentDecodeException is raised if another MIME type is given.

Parameters:
  • filename (str) – Filename to guess the MIME type from. If a MIME type is given, the filename is ignored.
  • mimetype (str) – MIME type to lookup, or None to guess the type from the filename.
Return type:

DocumentDecoder

load(filename, file=None, mimetype=None)

Load a document from a file.

Parameters:
  • filename (str) – Filename of document to load.
  • file (file-like object) – File object containing encoded data. If omitted, filename is loaded from disk.
  • mimetype (str) – MIME type of the document. If omitted, the filename extension is used to guess a MIME type. See get_decoder for a list of supported MIME types.
Return type:

AbstractDocument

Previous topic

pyglet.sprite

Next topic

pyglet.text.caret