ยปCore Development>Code coverage>Lib/tkinter/font.py

Python code coverage for Lib/tkinter/font.py

#countcontent
1n/a# Tkinter font wrapper
2n/a#
3n/a# written by Fredrik Lundh, February 1998
4n/a#
5n/a
6n/a__version__ = "0.9"
7n/a
8n/aimport itertools
9n/aimport tkinter
10n/a
11n/a
12n/a# weight/slant
13n/aNORMAL = "normal"
14n/aROMAN = "roman"
15n/aBOLD = "bold"
16n/aITALIC = "italic"
17n/a
18n/a
19n/adef nametofont(name):
20n/a """Given the name of a tk named font, returns a Font representation.
21n/a """
22n/a return Font(name=name, exists=True)
23n/a
24n/a
25n/aclass Font:
26n/a """Represents a named font.
27n/a
28n/a Constructor options are:
29n/a
30n/a font -- font specifier (name, system font, or (family, size, style)-tuple)
31n/a name -- name to use for this font configuration (defaults to a unique name)
32n/a exists -- does a named font by this name already exist?
33n/a Creates a new named font if False, points to the existing font if True.
34n/a Raises _tkinter.TclError if the assertion is false.
35n/a
36n/a the following are ignored if font is specified:
37n/a
38n/a family -- font 'family', e.g. Courier, Times, Helvetica
39n/a size -- font size in points
40n/a weight -- font thickness: NORMAL, BOLD
41n/a slant -- font slant: ROMAN, ITALIC
42n/a underline -- font underlining: false (0), true (1)
43n/a overstrike -- font strikeout: false (0), true (1)
44n/a
45n/a """
46n/a
47n/a counter = itertools.count(1)
48n/a
49n/a def _set(self, kw):
50n/a options = []
51n/a for k, v in kw.items():
52n/a options.append("-"+k)
53n/a options.append(str(v))
54n/a return tuple(options)
55n/a
56n/a def _get(self, args):
57n/a options = []
58n/a for k in args:
59n/a options.append("-"+k)
60n/a return tuple(options)
61n/a
62n/a def _mkdict(self, args):
63n/a options = {}
64n/a for i in range(0, len(args), 2):
65n/a options[args[i][1:]] = args[i+1]
66n/a return options
67n/a
68n/a def __init__(self, root=None, font=None, name=None, exists=False,
69n/a **options):
70n/a if not root:
71n/a root = tkinter._default_root
72n/a tk = getattr(root, 'tk', root)
73n/a if font:
74n/a # get actual settings corresponding to the given font
75n/a font = tk.splitlist(tk.call("font", "actual", font))
76n/a else:
77n/a font = self._set(options)
78n/a if not name:
79n/a name = "font" + str(next(self.counter))
80n/a self.name = name
81n/a
82n/a if exists:
83n/a self.delete_font = False
84n/a # confirm font exists
85n/a if self.name not in tk.splitlist(tk.call("font", "names")):
86n/a raise tkinter._tkinter.TclError(
87n/a "named font %s does not already exist" % (self.name,))
88n/a # if font config info supplied, apply it
89n/a if font:
90n/a tk.call("font", "configure", self.name, *font)
91n/a else:
92n/a # create new font (raises TclError if the font exists)
93n/a tk.call("font", "create", self.name, *font)
94n/a self.delete_font = True
95n/a self._tk = tk
96n/a self._split = tk.splitlist
97n/a self._call = tk.call
98n/a
99n/a def __str__(self):
100n/a return self.name
101n/a
102n/a def __eq__(self, other):
103n/a return isinstance(other, Font) and self.name == other.name
104n/a
105n/a def __getitem__(self, key):
106n/a return self.cget(key)
107n/a
108n/a def __setitem__(self, key, value):
109n/a self.configure(**{key: value})
110n/a
111n/a def __del__(self):
112n/a try:
113n/a if self.delete_font:
114n/a self._call("font", "delete", self.name)
115n/a except Exception:
116n/a pass
117n/a
118n/a def copy(self):
119n/a "Return a distinct copy of the current font"
120n/a return Font(self._tk, **self.actual())
121n/a
122n/a def actual(self, option=None, displayof=None):
123n/a "Return actual font attributes"
124n/a args = ()
125n/a if displayof:
126n/a args = ('-displayof', displayof)
127n/a if option:
128n/a args = args + ('-' + option, )
129n/a return self._call("font", "actual", self.name, *args)
130n/a else:
131n/a return self._mkdict(
132n/a self._split(self._call("font", "actual", self.name, *args)))
133n/a
134n/a def cget(self, option):
135n/a "Get font attribute"
136n/a return self._call("font", "config", self.name, "-"+option)
137n/a
138n/a def config(self, **options):
139n/a "Modify font attributes"
140n/a if options:
141n/a self._call("font", "config", self.name,
142n/a *self._set(options))
143n/a else:
144n/a return self._mkdict(
145n/a self._split(self._call("font", "config", self.name)))
146n/a
147n/a configure = config
148n/a
149n/a def measure(self, text, displayof=None):
150n/a "Return text width"
151n/a args = (text,)
152n/a if displayof:
153n/a args = ('-displayof', displayof, text)
154n/a return self._tk.getint(self._call("font", "measure", self.name, *args))
155n/a
156n/a def metrics(self, *options, **kw):
157n/a """Return font metrics.
158n/a
159n/a For best performance, create a dummy widget
160n/a using this font before calling this method."""
161n/a args = ()
162n/a displayof = kw.pop('displayof', None)
163n/a if displayof:
164n/a args = ('-displayof', displayof)
165n/a if options:
166n/a args = args + self._get(options)
167n/a return self._tk.getint(
168n/a self._call("font", "metrics", self.name, *args))
169n/a else:
170n/a res = self._split(self._call("font", "metrics", self.name, *args))
171n/a options = {}
172n/a for i in range(0, len(res), 2):
173n/a options[res[i][1:]] = self._tk.getint(res[i+1])
174n/a return options
175n/a
176n/a
177n/adef families(root=None, displayof=None):
178n/a "Get font families (as a tuple)"
179n/a if not root:
180n/a root = tkinter._default_root
181n/a args = ()
182n/a if displayof:
183n/a args = ('-displayof', displayof)
184n/a return root.tk.splitlist(root.tk.call("font", "families", *args))
185n/a
186n/a
187n/adef names(root=None):
188n/a "Get names of defined fonts (as a tuple)"
189n/a if not root:
190n/a root = tkinter._default_root
191n/a return root.tk.splitlist(root.tk.call("font", "names"))
192n/a
193n/a
194n/a# --------------------------------------------------------------------
195n/a# test stuff
196n/a
197n/aif __name__ == "__main__":
198n/a
199n/a root = tkinter.Tk()
200n/a
201n/a # create a font
202n/a f = Font(family="times", size=30, weight=NORMAL)
203n/a
204n/a print(f.actual())
205n/a print(f.actual("family"))
206n/a print(f.actual("weight"))
207n/a
208n/a print(f.config())
209n/a print(f.cget("family"))
210n/a print(f.cget("weight"))
211n/a
212n/a print(names())
213n/a
214n/a print(f.measure("hello"), f.metrics("linespace"))
215n/a
216n/a print(f.metrics(displayof=root))
217n/a
218n/a f = Font(font=("Courier", 20, "bold"))
219n/a print(f.measure("hello"), f.metrics("linespace", displayof=root))
220n/a
221n/a w = tkinter.Label(root, text="Hello, world", font=f)
222n/a w.pack()
223n/a
224n/a w = tkinter.Button(root, text="Quit!", command=root.destroy)
225n/a w.pack()
226n/a
227n/a fb = Font(font=w["font"]).copy()
228n/a fb.config(weight=BOLD)
229n/a
230n/a w.config(font=fb)
231n/a
232n/a tkinter.mainloop()