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