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( ) 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,
为给定文档创建标签。
- 参数:
document (
AbstractDocument
) -- 要附加到布局的文档。x (
float
) -- 标签的X坐标。y (
float
) -- 标签的Y坐标。z (
float
) -- 标签的Z坐标。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
,标签将进行文字包装并接受新元素。您还必须设置标签的宽度。program (
ShaderProgram
|None
) -- 可选使用的图形着色器。将影响所有字形。init_document (
bool
) -- 如果True
,文档将被初始化。如果您正在传递已经初始化的文档,则可以通过将其设置为来避免重复工作False
。
- get_style(name: str) Any
按名称获取文档样式值。
如果文档具有多个命名样式的值, pyglet.text.document.STYLE_INDETERMINATE 是返回的。
- property font_name: str | list[str]
字体系列名称。
传递给的字体名称
pyglet.font.load()
。可以选择提供名称列表:将使用第一个匹配的字体。
- 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,
使用HTML字符串创建标签。
- 参数:
text (
str
) -- 要显示的文本。location (
Location
|None
) -- 用于加载文档中引用的图像的位置对象。默认情况下,使用工作目录。x (
float
) -- 标签的X坐标。y (
float
) -- 标签的Y坐标。z (
float
) -- 标签的Z坐标。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,则标签将进行单词包装并接受新元素。 您还必须设置标签的宽度。program (
ShaderProgram
|None
) -- 可选使用的图形着色器。将影响所有字形。
- 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,
创建纯文本标签。
- 参数:
text (
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坐标。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,则标签将进行单词包装并接受新元素。 您还必须设置标签的宽度。program (
ShaderProgram
|None
) -- 可选使用的图形着色器。将影响所有字形。
- decode_attributed(text: str) FormattedDocument
直接从一些属性文本创建文档。
看见 pyglet.text.formats.attributed 有关属性文本的说明,请参见。
- 返回类型:
- decode_html( ) FormattedDocument
直接从一些HTML格式的文本创建文档。
- 参数:
- 返回类型:
- decode_text(text: str) UnformattedDocument
直接从一些纯文本创建文档。
- 返回类型:
- get_decoder(
- filename: str | None,
- mimetype: Literal['text/plain', 'text/html', 'text/vnd.pyglet-attributed'] | None = None,
获取给定文件名和MIME类型的文档解码器。
如果
mimetype
被省略,则从文件扩展名中猜测。支持以下MIME类型:
text/plain
纯文本
text/html
HTML4过渡版
text/vnd.pyglet-attributed
属性文本;请参见 pyglet.text.formats.attributed
- 参数:
- 抛出:
DocumentDecodeException -- 如果SME类型不是来自支持的类型。
- 返回类型: