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)

類別 Font 代表一個已命名字型。Font 實例會被賦予一個的名字,也可以特指他們的字型家族 (font family)、字級 (size)、以及外觀設定。已命名字型是 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 - 字型家族,例如:Courier、Times
size - 字級
如果 size 是正數則會直譯成以點 (point) 為單位的字級。
如果 size 是負數則會變成絕對值
以像素 (pixel) 為單位的字級。
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)

回傳目前字型被格式化時,在特定顯示區域中文字所用的空間(以整數像素為單位)。若顯示區域沒有被指定,則會假定主程式視窗為顯示區域。

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)

回傳可用字型家族名稱的元組。

tkinter.font.names(root=None)

回傳所有已定義字型名稱的元組。

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 參數。