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 を指定しなければすべての属性の辞書を返し、指定した場合はその属性の値だけを返します。 属性は 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)

フォントの属性を 1 つ以上変更します。 引数がなければ、現在の属性の辞書を返します。

config() は configure() の別名です。

copy()

現在のフォントの独立したコピーを返します。 属性は同じで名前が異なる新しい名前付きフォントで、元のフォントとは別に設定を変更できます。 現在のフォントがフォント記述をラップしている場合、コピーはその属性を解決した名前付きフォントになります。

measure(text, displayof=None)

指定したディスプレイで、現在のフォントを使って text を表示した場合に必要な幅を、ピクセル単位の整数で返します。 ディスプレイを指定しなければ、メインのアプリケーションウィンドウを使います。

metrics(*options, **kw)

フォント固有のデータを返します。 オプションがなければ、各メトリックの名前を整数値に対応付ける辞書を返します。 オプション名を 1 つ指定すると、そのメトリックの値を整数で返します。 オプションには次のものがあります:

ascent - ベースラインから、このフォントの文字が占める最も高い位置までの距離

descent - ベースラインから、このフォントの文字が占める最も低い位置までの距離

linespace - 行の文字が上下に重ならないようにするために、このフォントの文字同士に必要な最小の垂直方向の間隔。

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 引数が追加されました。