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:
objectNamespace 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-levelset_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.
- classmethod create_alias(name, **opts)#
Register a user named font
nameand return itsfont.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 standardTk*Fontnames use. Returns the wrapper for further tweaking; re-registering a name reconfigures it.
- 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 (fromnames()) to its actual options. The “see what the fonts actually are” helper; fonts absent on the platform are omitted from the full mapping.
- classmethod names()#
Return the standard named fonts this utility manages (see
describe).
- classmethod reset()#
Drop the cached
font.Fontwrappers.Called from
App.destroyso wrappers pinned to a destroyed root are not reused by a later root (the same singleton/root-rebind hazardStyleclears). 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
familyin one call.The headline typography one-liner: because widgets read the standard named fonts, this restyles the whole app.
TkFixedFont(the monospace font, used byText/console-style widgets) is left alone unlessmono_familyis given. Named fonts absent on the current platform are skipped.
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
Fontobject for a named font, so you can query or reconfigure it.- Parameters:
name – the named font, e.g.
"TkDefaultFont".- Returns:
a
Fontbound 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
fontoption. Build one fromoptions—family,size(points; negative is pixels),weight("normal"/"bold"),slant("roman"/"italic"),underline,overstrike— or from an existing spec viafont. Passname(withexists=True) to bind to a named font.
- measure(text, displayof=None)
Measure how wide
textis 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, andfixed. 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
Fontwith the same attributes.- Returns:
a new
Font.
See also#
Typography — how to use fonts, with examples.