tkinter.font --- Tkinter 字体包装器

源代码: Lib/tkinter/font.py


tkinter.font 模块提供了 Font 类,用于创建和使用命名字体。

不同的字体粗细和倾斜是:

tkinter.font.NORMAL
tkinter.font.BOLD
tkinter.font.ITALIC
tkinter.font.ROMAN
class tkinter.font.Font(root=None, font=None, name=None, exists=False, **options)

The Font class represents a font used by Tk widgets. It either creates a new named font or refers to an existing font. A named font is Tk's way of identifying a font as a single object that can be referred to by name and reconfigured in place, rather than respecifying its attributes at each use.

With exists false (the default), a new named font is created. Its attributes are taken from the font description font if it is given, overridden by any keyword options. The new font is named name, or a generated unique name if name is omitted.

With exists true, an existing font is referred to instead of being created. If name is given, it is the name of the font, which is reconfigured by font and options if either is given. If name is omitted, the font description font is wrapped as is, without creating a named font, so that it is used without loss of precision by actual(), measure() and metrics(). In this case no keyword options are accepted, and the name attribute is the description itself rather than a string.

The font description font is a tuple of the family name, the size and zero or more styles, or any other form accepted by Tk, such as the name of a named font.

The keyword options are:

family - 实体族,例如 Courier, Times
size - 字体大小
如果 size 为正数,则解释为以磅为单位的大小。
如果 size 是负数,则将其绝对值
解释为以像素为单位的大小。
weight - 字体强调 (NORMAL, BOLD)(普通,加粗)
slant - ROMAN, ITALIC(正体,斜体)
underline - 字体下划线(0 - 无下划线,1 - 有下划线)
overstrike - 字体删除线(0 - 无删除线,1 - 有删除线)

在 3.10 版本发生变更: Two fonts now compare equal (==) only when both are Font instances with the same name belonging to the same Tcl interpreter.

在 3.16 版本发生变更: A font description can now be wrapped without creating a new named font, and keyword options now override the attributes of the specified font.

actual(option=None, displayof=None)

返回字体的实际属性,由于平台限制,这些属性可能与请求的属性不同。在没有 option 的情况下,返回包含所有属性的字典;如果给定了 option ,则返回该单个属性的值。这些属性是在 displayof 部件的显示上解析的,如果未指定该部件,则在主应用程序窗口上解析。

cget(option)

检索字体的某一个属性值。

备注

cget() and configure() operate on a named font and raise TclError for a wrapped font description. Use actual() to query the attributes of the latter.

configure(**options)

修改字体的一个或多个属性。在不带参数的情况下,返回当前属性的字典。

config() 是 configure() 的别名。

copy()

返回当前字体的独立副本:具有相同属性但名称不同的新命名字体,可以独立于原始字体进行重新配置。如果当前字体包装了一个字体描述,则该副本是具有其已解析属性的命名字体。

measure(text, displayof=None)

以整数像素数返回使用当前字体排版时文本在指定显示上所占用的空间量。如果未指定显示,则默认使用主应用程序窗口。

metrics(*options, **kw)

返回字体专属数据。在没有选项的情况下,返回将每个度量名称映射到其整数值的字典;如果给出了一个选项名称,则以整数形式返回该度量的值。选项包括:

ascent - distance between baseline and highest point that a character of the font can occupy

descent - distance between baseline and lowest point that a character of the font can occupy

linespace - minimum vertical separation necessary between any two characters of the font that ensures no vertical overlap between lines.

fixed - 如果该字体是等宽字体则为 1,否则为 0。

tkinter.font.families(root=None, displayof=None)

返回可用字体系列名称的元组。

tkinter.font.names(root=None)

返回所有已定义字体名称的元组。

tkinter.font.nametofont(name, root=None)

返回已有命名字体 name 的 Font 表示形式。 root 是其 Tcl 解释器拥有该字体的部件;如果省略,则使用默认的根窗口。

在 3.10 版本发生变更: 增加了 root 形参。