tkinter.font --- Tkinter 字体包装器¶
源代码: Lib/tkinter/font.py
tkinter.font 模块提供了 Font 类,用于创建和使用命名字体。
不同的字体粗细和倾斜是:
- class tkinter.font.Font(root=None, font=None, name=None, exists=False, **options)¶
The
Fontclass 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()andmetrics(). In this case no keyword options are accepted, and thenameattribute 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, Timessize - 字体大小如果 size 为正数,则解释为以磅为单位的大小。如果 size 是负数,则将其绝对值解释为以像素为单位的大小。weight - 字体强调 (NORMAL, BOLD)(普通,加粗)slant - ROMAN, ITALIC(正体,斜体)underline - 字体下划线(0 - 无下划线,1 - 有下划线)overstrike - 字体删除线(0 - 无删除线,1 - 有删除线)在 3.10 版本发生变更: Two fonts now compare equal (
==) only when both areFontinstances with the same name belonging to the same Tcl interpreter.在 3.16.0a0 (unreleased) 版本发生变更: 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)¶
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. The attributes are resolved on the display of the displayof widget, or the main application window if it is not specified.
- cget(option)¶
检索字体的某一个属性值。
备注
cget()andconfigure()operate on a named font and raiseTclErrorfor a wrapped font description. Useactual()to query the attributes of the latter.
- configure(**options)¶
Modify one or more attributes of the font. With no arguments, return a dictionary of the current attributes.
config()是configure()的别名。
- copy()¶
Return a distinct copy of the current font: a new named font with the same attributes but a different name, which can be reconfigured independently of the original. If the current font wraps a font description, the copy is instead a named font with its resolved attributes.
- 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.