blob: 30c1e7b5f9eb434a6951b6b089e690f54429035e [file] [log] [blame]
Nikhil80428ed2019-09-10 01:55:34 -07001:mod:`tkinter.font` --- Tkinter font wrapper
2============================================
3
4.. module:: tkinter.font
5 :platform: Tk
6 :synopsis: Tkinter font-wrapping class
7
8**Source code:** :source:`Lib/tkinter/font.py`
9
10--------------
11
12The :mod:`tkinter.font` module provides the :class:`Font` class for creating
13and using named fonts.
14
15The different font weights and slants are:
16
17.. data:: NORMAL
18 BOLD
19 ITALIC
20 ROMAN
21
22.. class:: Font(root=None, font=None, name=None, exists=False, **options)
23
24 The :class:`Font` class represents a named font. *Font* instances are given
25 unique names and can be specified by their family, size, and style
26 configuration. Named fonts are Tk's method of creating and identifying
27 fonts as a single object, rather than specifying a font by its attributes
28 with each occurrence.
29
30 arguments:
31
32 | *font* - font specifier tuple (family, size, options)
33 | *name* - unique font name
34 | *exists* - self points to existing named font if true
35
36 additional keyword options (ignored if *font* is specified):
37
38 | *family* - font family i.e. Courier, Times
39 | *size* - font size
40 | If *size* is positive it is interpreted as size in points.
41 | If *size* is a negative number its absolute value is treated as
42 as size in pixels.
43 | *weight* - font emphasis (NORMAL, BOLD)
44 | *slant* - ROMAN, ITALIC
45 | *underline* - font underlining (0 - none, 1 - underline)
46 | *overstrike* - font strikeout (0 - none, 1 - strikeout)
47
48 .. method:: actual(option=None, displayof=None)
49
50 Return the attributes of the font.
51
52 .. method:: cget(option)
53
54 Retrieve an attribute of the font.
55
56 .. method:: config(**options)
57
58 Modify attributes of the font.
59
60 .. method:: copy()
61
62 Return new instance of the current font.
63
64 .. method:: measure(text, displayof=None)
65
66 Return amount of space the text would occupy on the specified display
67 when formatted in the current font. If no display is specified then the
68 main application window is assumed.
69
70 .. method:: metrics(*options, **kw)
71
72 Return font-specific data.
73 Options include:
74
75 *ascent* - distance between baseline and highest point that a
76 character of the font can occupy
77
78 *descent* - distance between baseline and lowest point that a
79 character of the font can occupy
80
81 *linespace* - minimum vertical separation necessary between any two
82 characters of the font that ensures no vertical overlap between lines.
83
84 *fixed* - 1 if font is fixed-width else 0
85
86.. function:: families(root=None, displayof=None)
87
88 Return the different font families.
89
90.. function:: names(root=None)
91
92 Return the names of defined fonts.
93
94.. function:: nametofont(name)
95
96 Return a :class:`Font` representation of a tk named font.