pyglet.text

子模块

细节

文本格式、布局和显示。

该模块提供了用于从文本文件、HTML文件和特定于pyglet的标记格式加载样式文档的类。文档可以使用多种字体、颜色、样式、文本大小、页边距、段落对齐等设置样式。

使用Layout类,可以在单行上对文档进行布局,也可以对文档进行自动换行以适合矩形。然后,可以在窗口中高效地绘制布局或增量地更新布局(例如,以支持交互式文本编辑)。

对于应用程序只需要在窗口中显示一些文本的常见情况,标签类提供了一个简单的接口。

可以使用::创建纯文本标签

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

或者,可以使用以下命令创建使用HTML的带样式的文本标签:

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

然后,可以随时使用以下命令绘制这两个标签:

label.draw()

有关支持的HTML子集的详细信息,请参见 pyglet.text.formats.html

有关文档和布局类的高级用法,包括交互编辑、在文档中嵌入对象和创建可滚动布局,请参阅《编程指南》。

exception DocumentDecodeException

对文档文本进行解码时出错。

class DocumentDecoder

摘要文档解码器。

abstract decode(
text: str,
location: Location | None = None,
) AbstractDocument

对文档文本进行解码。

参数:
  • text (str) -- 要解码的文本

  • location (Location | None) -- 用作文档中引用的其他资源(例如,HTML图像)的基本路径的位置。

返回类型:

AbstractDocument

class DocumentLabel

基本标签分类。

标签是一种布局,它公开了操作关联文档的方便方法。

__init__(
document: AbstractDocument,
x: float = 0.0,
y: float = 0.0,
z: float = 0.0,
width: int | None = None,
height: int | None = None,
anchor_x: Literal['left', 'center', 'right'] = 'left',
anchor_y: Literal['top', 'bottom', 'center', 'baseline'] = 'baseline',
rotation: float = 0.0,
multiline: bool = False,
dpi: int | None = None,
batch: Batch | None = None,
group: Group | None = None,
program: ShaderProgram | None = None,
init_document: bool = True,
) None

为给定文档创建标签。

参数:
  • document (AbstractDocument) -- 要附加到布局的文档。

  • x (float) -- 标签的X坐标。

  • y (float) -- 标签的Y坐标。

  • z (float) -- 标签的Z坐标。

  • width (int | None) -- 标签宽度(以像素为单位),或 None

  • height (int | None) -- 标签的高度(以像素为单位),或 None

  • anchor_x (Literal['left', 'center', 'right']) -- X坐标的锚点:其中之一 "left""center" 或` "right"

  • anchor_y (Literal['top', 'bottom', 'center', 'baseline']) -- Y坐标的锚点:其中之一 "bottom""baseline""center""top"

  • rotation (float) -- 以度数为单位旋转标签的量。正值将是顺时针旋转,负值将导致逆时针旋转。

  • multiline (bool) -- 如果 True ,标签将进行文字包装并接受新元素。您还必须设置标签的宽度。

  • dpi (int | None) -- 此布局中字体的分辨率。切换到96。

  • batch (Batch | None) -- 可选要添加标签的图形批次。

  • group (Group | None) -- 可选使用的图形组。

  • program (ShaderProgram | None) -- 可选使用的图形着色器。将影响所有字形。

  • init_document (bool) -- 如果 True ,文档将被初始化。如果您正在传递已经初始化的文档,则可以通过将其设置为来避免重复工作 False

get_style(name: str) Any

按名称获取文档样式值。

如果文档具有多个命名样式的值, pyglet.text.document.STYLE_INDETERMINATE 是返回的。

参数:

name (str) -- 要查询的样式名称。 请参阅来自 pyglet.text.layout 用于已知的样式名称。

返回类型:

Any

set_style(name: str, value: Any) None

按名称设置整个文档的文档样式值。

参数:
  • name (str) -- 要设置的样式的名称。请参阅的文档 pyglet.text.layout 用于已知的样式名称。

  • value (Any) -- 样式的值。

返回类型:

None

property bold: bool | str

粗体字体。

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

文本颜色。

颜色是RGBA组件的四元组,每个组件都在范围内 [0, 255] 。

property font_name: str | list[str]

字体系列名称。

传递给的字体名称 pyglet.font.load() 。可以选择提供名称列表:将使用第一个匹配的字体。

property font_size: float

字体大小,以磅为单位。

property italic: bool | str

斜体字体样式。

property opacity: int

混合不透明度。

此属性设置标签顶点颜色的Alpha分量。使用默认混合模式时,这允许以部分不透明度绘制布局,与背景混合。

不透明度为255(默认值)没有任何效果。不透明度为128将使标签显示为半透明。

property text: str

标签的文本。

class HTMLLabel

HTML格式的文本标签。

支持HTML4.01的子集。看见 pyglet.text.formats.html 了解更多细节。

__init__(
text: str = '',
location: Location | None = None,
x: float = 0.0,
y: float = 0.0,
z: float = 0.0,
width: int | None = None,
height: int | None = None,
anchor_x: Literal['left', 'center', 'right'] = 'left',
anchor_y: Literal['top', 'bottom', 'center', 'baseline'] = 'baseline',
rotation: float = 0.0,
multiline: bool = False,
dpi: int | None = None,
batch: Batch | None = None,
group: Group | None = None,
program: ShaderProgram | None = None,
) None

使用HTML字符串创建标签。

参数:
  • text (str) -- 要显示的文本。

  • location (Location | None) -- 用于加载文档中引用的图像的位置对象。默认情况下,使用工作目录。

  • x (float) -- 标签的X坐标。

  • y (float) -- 标签的Y坐标。

  • z (float) -- 标签的Z坐标。

  • width (int | None) -- 标签宽度(以像素为单位),或无

  • height (int | None) -- 标签高度(以像素为单位),或无

  • anchor_x (Literal['left', 'center', 'right']) -- X坐标的锚点:其中之一 "left""center""right"

  • anchor_y (Literal['top', 'bottom', 'center', 'baseline']) -- Y坐标的锚点:其中之一 "bottom""baseline""center""top"

  • rotation (float) -- 以度数为单位旋转标签的量。正值将是顺时针旋转,负值将导致逆时针旋转。

  • multiline (bool) -- 如果为True,则标签将进行单词包装并接受新元素。 您还必须设置标签的宽度。

  • dpi (int | None) -- 此布局中字体的分辨率。 切换到96。

  • batch (Batch | None) -- 可选要添加标签的图形批次。

  • group (Group | None) -- 可选使用的图形组。

  • program (ShaderProgram | None) -- 可选使用的图形着色器。将影响所有字形。

property text: str

标签的HTML格式文本。

class Label

纯文本标签。

__init__(
text: str = '',
x: float = 0.0,
y: float = 0.0,
z: float = 0.0,
width: int | None = None,
height: int | None = None,
anchor_x: Literal['left', 'center', 'right'] = 'left',
anchor_y: Literal['top', 'bottom', 'center', 'baseline'] = 'baseline',
rotation: float = 0.0,
multiline: bool = False,
dpi: int | None = None,
font_name: str | None = None,
font_size: float | None = None,
bold: bool | str = False,
italic: bool | str = False,
stretch: bool | str = False,
color: tuple[int, int, int, int] | tuple[int, int, int] = (255, 255, 255, 255),
align: Literal['left', 'center', 'top'] = 'left',
batch: Batch | None = None,
group: Group | None = None,
program: ShaderProgram | None = None,
) None

创建纯文本标签。

参数:
  • text (str) -- 要显示的文本。

  • font_name (str | None) -- 字体家族名称。 如果给出了多个名称,则使用第一个匹配的名称。

  • font_size (float | None) -- 字体大小,以磅为单位。

  • bold (bool | str) -- 粗体字体。

  • italic (bool | str) -- 斜体字体样式。

  • stretch (bool | str) -- 拉伸字体样式。

  • color (tuple[int, int, int, int] | tuple[int, int, int]) -- 字体颜色为RGBA或RB组件,每个组件都在 0 <= component <= 255

  • x (float) -- 标签的X坐标。

  • y (float) -- 标签的Y坐标。

  • z (float) -- 标签的Z坐标。

  • width (int | None) -- 标签宽度(以像素为单位),或无

  • height (int | None) -- 标签高度(以像素为单位),或无

  • anchor_x (Literal['left', 'center', 'right']) -- X坐标的锚点:其中之一 "left""center""right"

  • anchor_y (Literal['top', 'bottom', 'center', 'baseline']) -- Y坐标的锚点:其中之一 "bottom""baseline""center""top"

  • rotation (float) -- 以度数为单位旋转标签的量。正值将是顺时针旋转,负值将导致逆时针旋转。

  • align (Literal['left', 'center', 'top']) -- 行上文本的水平对齐方式仅在提供宽度时适用。之一 "left""center""right"

  • multiline (bool) -- 如果为True,则标签将进行单词包装并接受新元素。 您还必须设置标签的宽度。

  • dpi (int | None) -- 此布局中字体的分辨率。 切换到96。

  • batch (Batch | None) -- 可选要添加标签的图形批次。

  • group (Group | None) -- 可选使用的图形组。

  • program (ShaderProgram | None) -- 可选使用的图形着色器。将影响所有字形。

decode_attributed(text: str) FormattedDocument

直接从一些属性文本创建文档。

看见 pyglet.text.formats.attributed 有关属性文本的说明,请参见。

返回类型:

FormattedDocument

decode_html(
text: str,
location: str | None = None,
) FormattedDocument

直接从一些HTML格式的文本创建文档。

参数:
  • text (str) -- 要解码的HTML数据。

  • location (str | None) -- 提供从文档引用的附加资源(例如,图像)的基本路径的位置。

返回类型:

FormattedDocument

decode_text(text: str) UnformattedDocument

直接从一些纯文本创建文档。

返回类型:

UnformattedDocument

get_decoder(
filename: str | None,
mimetype: Literal['text/plain', 'text/html', 'text/vnd.pyglet-attributed'] | None = None,
) DocumentDecoder

获取给定文件名和MIME类型的文档解码器。

如果 mimetype 被省略,则从文件扩展名中猜测。

支持以下MIME类型:

text/plain

纯文本

text/html

HTML4过渡版

text/vnd.pyglet-attributed

属性文本;请参见 pyglet.text.formats.attributed

参数:
  • filename (str | None) -- 要从中猜测MIME类型的文件名。如果给定了MIME类型,则忽略该文件名。

  • mimetype (Literal['text/plain', 'text/html', 'text/vnd.pyglet-attributed'] | None) -- 要查找的MIME类型,或 None 从文件名猜测类型。

抛出:

DocumentDecodeException -- 如果SME类型不是来自支持的类型。

返回类型:

DocumentDecoder

load(
filename: str,
file: BinaryIO | None = None,
mimetype: Literal['text/plain', 'text/html', 'text/vnd.pyglet-attributed'] | None = None,
) AbstractDocument

从文件加载文档。

参数:
  • filename (str) -- 要加载的文档的文件名。

  • file (BinaryIO | None) -- 包含编码数据的文件对象。如果省略, filename 是从磁盘加载的。

  • mimetype (Literal['text/plain', 'text/html', 'text/vnd.pyglet-attributed'] | None) -- 文档的MIME类型。如果省略,则使用文件扩展名来猜测MIME类型。看见 get_decoder 有关支持的MIME类型的列表。

返回类型:

AbstractDocument