Fonts#

Working with fonts spans two surfaces: the Fonts manager for the application’s named fonts (the shared TkDefaultFont/TkTextFont/… that widgets use, and the global family), and the font tools — Font, font_families, nametofont — for listing families, measuring text, and building individual font objects. The Typography guide shows them in use.

The Fonts manager#

class Fonts#

Bases: object

Namespace of typography helpers over the standard Tk named fonts.

Every method operates on the live application root, so call them once App() exists. For the pre-root “set it at the top of the file” case use the module-level set_global_family, which rides the deferred-config seam.

classmethod configure(name, **opts)#

Tweak a single named font (family/size/weight/slant/…).

Thin wrapper over font.nametofont(name).configure(**opts); the change is seen live by every widget that reads that named font.

Parameters:
Return type:

None

classmethod create_alias(name, **opts)#

Register a user named font name and return its font.Font.

A convenience over font.Font(name=name, ...) so an app can define its own named font once and refer to it by name (font="Body") everywhere, the same seam the standard Tk*Font names use. Returns the wrapper for further tweaking; re-registering a name reconfigures it.

Parameters:
Return type:

Font

classmethod describe(name=None)#

Return each named font’s resolved family/size/weight/slant/… .

With name, return the one font’s actual options; otherwise a mapping of every managed named font (from names()) to its actual options. The “see what the fonts actually are” helper; fonts absent on the platform are omitted from the full mapping.

Parameters:

name (str | None)

Return type:

dict

classmethod names()#

Return the standard named fonts this utility manages (see describe).

Return type:

tuple[str, …]

classmethod reset()#

Drop the cached font.Font wrappers.

Called from App.destroy so wrappers pinned to a destroyed root are not reused by a later root (the same singleton/root-rebind hazard Style clears). The named fonts themselves live in the interpreter and die with it; this only clears our Python-side cache.

Return type:

None

classmethod set_global_family(family, *, mono_family=None)#

Retint every proportional named font to family in one call.

The headline typography one-liner: because widgets read the standard named fonts, this restyles the whole app. TkFixedFont (the monospace font, used by Text/console-style widgets) is left alone unless mono_family is given. Named fonts absent on the current platform are skipped.

Parameters:
  • family (str)

  • mono_family (str | None)

Return type:

None

Font families and named fonts#

font_families(root=None, displayof=None)

List the font families installed on the system — the values usable as a font’s family.

Returns:

a tuple of family names.

nametofont(name, root=None)

Return the Font object for a named font, so you can query or reconfigure it.

Parameters:

name – the named font, e.g. "TkDefaultFont".

Returns:

a Font bound to that named font.

The Font object#

class Font(font=None, name=None, exists=False, **options)

A font you can measure with and pass to a widget’s font option. Build one from optionsfamily, size (points; negative is pixels), weight ("normal"/"bold"), slant ("roman"/"italic"), underline, overstrike — or from an existing spec via font. Pass name (with exists=True) to bind to a named font.

measure(text, displayof=None)

Measure how wide text is in this font.

Parameters:

text – the string to measure.

Returns:

the width in pixels (int).

metrics(*options, displayof=None)

Report the font’s vertical metrics — ascent, descent, linespace, and fixed. Pass one option name to get that value alone.

Returns:

a dict of all metrics, or a single value when one option is named.

actual(option=None, displayof=None)

Report the concrete attributes this font resolves to (useful for aliases like "TkDefaultFont"). Pass one option name to get that value alone.

Returns:

a dict of the actual attributes, or a single value.

configure(**options)

Get or set the font’s attributes (family, size, weight, …). A named font reconfigured this way updates every widget using it. Alias: config.

Returns:

the current options when called with no arguments, otherwise None.

cget(option)

Return the value of one font attribute.

Parameters:

option – the attribute name, e.g. "size".

Returns:

the attribute’s value.

copy()

Make a new Font with the same attributes.

Returns:

a new Font.

See also#