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)

Font 类表示命名字体。Font 实例具有唯一的名称,可以通过其族、大小和样式配置进行指定。命名字体是 Tk 将字体创建和标识为单个对象的方法,而不是通过每次出现时的属性来指定字体。

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

参数:

font - 字体指示符元组 (family, size, options)
name - 唯一的字体名
exists - 指向现有命名字体(如果有)

其他关键字选项(如果指定了 font,则忽略):

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

Return the actual attributes of the font, which may differ from the requested ones because of platform limitations. With no option, return a dictionary of all the attributes; if option is given, return the value of that single attribute.

cget(option)

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

configure(**options)

Modify one or more attributes of the font. With no arguments, return a dictionary of the current attributes.

config() is an alias of configure().

copy()

返回当前字体的新实例。

measure(text, displayof=None)

Return amount of space the text would occupy on the specified display when formatted in the current font, as an integer number of pixels. If no display is specified then the main application window is assumed.

metrics(*options, **kw)

Return font-specific data. With no options, return a dictionary mapping each metric name to its integer value; if one option name is given, return that metric's value as an integer. Options include:

ascent - 基线和最高点之间的距离

(在该字体中的一个字符可以占用的空间中)

descent - 基线和最低点之间的距离

(在该字体中的一个字符可以占用的空间中)

linespace - 所需最小垂直间距(在两个

该字体的字符间,使得这两个字符在垂直方向上不重叠)。

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

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

Return a tuple of the names of the available font families.

tkinter.font.names(root=None)

Return a tuple of the names of all the defined fonts.

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

Return a Font representation of the existing named font name. root is the widget whose Tcl interpreter owns the font; if omitted, the default root window is used.

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