tkinter.font --- Tkinter 字型包裝器

原始碼:Lib/tkinter/font.py


tkinter.font 模組提供類別 Font,可以建立及使用已命名的字型。

不同的字重 (font weights) 以及傾斜 (slant) 是:

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 - font family, for example, Courier, Times
size - 字級
如果 size 是正數則會直譯成以點 (point) 為單位的字級。
如果 size 是負數則會變成絕對值
以像素 (pixel) 為單位的字級。
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.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() 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)

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()

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 - 若字型為等寬 (fixed-width) 的則為 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 參數。