Mercurial > cpython-lru_cache-weakref
annotate Lib/tkinter/font.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 774c2afa6665 |
| children |
| rev | line source |
|---|---|
|
35858
6a018d8c59c9
r903@spiff: Fredrik | 2005-11-12 16:28:37 +0100
Fredrik Lundh <fredrik@pythonware.com>
parents:
33205
diff
changeset
|
1 # Tkinter font wrapper |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 # |
|
35858
6a018d8c59c9
r903@spiff: Fredrik | 2005-11-12 16:28:37 +0100
Fredrik Lundh <fredrik@pythonware.com>
parents:
33205
diff
changeset
|
3 # written by Fredrik Lundh, February 1998 |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 # |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 __version__ = "0.9" |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 |
|
76089
a77e23135675
Issue #802310: Generate always unique tkinter font names if not directly passed
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76088
diff
changeset
|
8 import itertools |
|
47130
ef4cca2d6171
Tkinter, step 2: adapt imports.
Georg Brandl <georg@python.org>
parents:
47129
diff
changeset
|
9 import tkinter |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 |
|
76088
db0063ce8a5c
reformat code to follow PEP8
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
64795
diff
changeset
|
11 |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 # weight/slant |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 NORMAL = "normal" |
|
29473
14a1547129fb
Patch #751107: Slant must be 'roman', not 'normal'.
Martin v. Löwis <martin@v.loewis.de>
parents:
28513
diff
changeset
|
14 ROMAN = "roman" |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 BOLD = "bold" |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 ITALIC = "italic" |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 |
|
76088
db0063ce8a5c
reformat code to follow PEP8
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
64795
diff
changeset
|
18 |
|
33170
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
19 def nametofont(name): |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
20 """Given the name of a tk named font, returns a Font representation. |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
21 """ |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
22 return Font(name=name, exists=True) |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33181
diff
changeset
|
23 |
|
76088
db0063ce8a5c
reformat code to follow PEP8
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
64795
diff
changeset
|
24 |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 class Font: |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 """Represents a named font. |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 Constructor options are: |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 font -- font specifier (name, system font, or (family, size, style)-tuple) |
|
33170
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
31 name -- name to use for this font configuration (defaults to a unique name) |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
32 exists -- does a named font by this name already exist? |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
33 Creates a new named font if False, points to the existing font if True. |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
34 Raises _tkinter.TclError if the assertion is false. |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 |
|
33170
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
36 the following are ignored if font is specified: |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 family -- font 'family', e.g. Courier, Times, Helvetica |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 size -- font size in points |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 weight -- font thickness: NORMAL, BOLD |
|
29473
14a1547129fb
Patch #751107: Slant must be 'roman', not 'normal'.
Martin v. Löwis <martin@v.loewis.de>
parents:
28513
diff
changeset
|
41 slant -- font slant: ROMAN, ITALIC |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 underline -- font underlining: false (0), true (1) |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 overstrike -- font strikeout: false (0), true (1) |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33181
diff
changeset
|
44 |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 """ |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 |
|
76089
a77e23135675
Issue #802310: Generate always unique tkinter font names if not directly passed
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76088
diff
changeset
|
47 counter = itertools.count(1) |
|
a77e23135675
Issue #802310: Generate always unique tkinter font names if not directly passed
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76088
diff
changeset
|
48 |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 def _set(self, kw): |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 options = [] |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 for k, v in kw.items(): |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 options.append("-"+k) |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 options.append(str(v)) |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 return tuple(options) |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 def _get(self, args): |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
8960
diff
changeset
|
57 options = [] |
|
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
8960
diff
changeset
|
58 for k in args: |
|
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
8960
diff
changeset
|
59 options.append("-"+k) |
|
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
8960
diff
changeset
|
60 return tuple(options) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 def _mkdict(self, args): |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 options = {} |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 for i in range(0, len(args), 2): |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 options[args[i][1:]] = args[i+1] |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 return options |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 |
|
76088
db0063ce8a5c
reformat code to follow PEP8
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
64795
diff
changeset
|
68 def __init__(self, root=None, font=None, name=None, exists=False, |
|
db0063ce8a5c
reformat code to follow PEP8
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
64795
diff
changeset
|
69 **options): |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 if not root: |
|
47130
ef4cca2d6171
Tkinter, step 2: adapt imports.
Georg Brandl <georg@python.org>
parents:
47129
diff
changeset
|
71 root = tkinter._default_root |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 if font: |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 # get actual settings corresponding to the given font |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 font = root.tk.splitlist(root.tk.call("font", "actual", font)) |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 else: |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 font = self._set(options) |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 if not name: |
|
76089
a77e23135675
Issue #802310: Generate always unique tkinter font names if not directly passed
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76088
diff
changeset
|
78 name = "font" + str(next(self.counter)) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 self.name = name |
|
33170
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
80 |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
81 if exists: |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
82 self.delete_font = False |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
83 # confirm font exists |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
84 if self.name not in root.tk.call("font", "names"): |
|
47130
ef4cca2d6171
Tkinter, step 2: adapt imports.
Georg Brandl <georg@python.org>
parents:
47129
diff
changeset
|
85 raise tkinter._tkinter.TclError( |
|
ef4cca2d6171
Tkinter, step 2: adapt imports.
Georg Brandl <georg@python.org>
parents:
47129
diff
changeset
|
86 "named font %s does not already exist" % (self.name,)) |
|
33170
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
87 # if font config info supplied, apply it |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
88 if font: |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
89 root.tk.call("font", "configure", self.name, *font) |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
90 else: |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
91 # create new font (raises TclError if the font exists) |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33181
diff
changeset
|
92 root.tk.call("font", "create", self.name, *font) |
|
33170
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
93 self.delete_font = True |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 # backlinks! |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 self._root = root |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 self._split = root.tk.splitlist |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 self._call = root.tk.call |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 def __str__(self): |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 return self.name |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 |
|
33170
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
102 def __eq__(self, other): |
|
64795
a431c48ad42b
#1730136: Fix comparison between a tk Font object and an object of a different type.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47130
diff
changeset
|
103 return isinstance(other, Font) and self.name == other.name |
|
33170
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
104 |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
105 def __getitem__(self, key): |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
106 return self.cget(key) |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
107 |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
108 def __setitem__(self, key, value): |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
109 self.configure(**{key: value}) |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
110 |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 def __del__(self): |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 try: |
|
33170
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
113 if self.delete_font: |
|
a5769e8f1c6f
Patch #764217: Add nametofont function, exists parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
29473
diff
changeset
|
114 self._call("font", "delete", self.name) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
35858
diff
changeset
|
115 except (KeyboardInterrupt, SystemExit): |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
35858
diff
changeset
|
116 raise |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
35858
diff
changeset
|
117 except Exception: |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 pass |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33181
diff
changeset
|
119 |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 def copy(self): |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 "Return a distinct copy of the current font" |
|
28513
90c77b8af1a4
SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents:
16517
diff
changeset
|
122 return Font(self._root, **self.actual()) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 |
|
76115
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
124 def actual(self, option=None, displayof=None): |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 "Return actual font attributes" |
|
76115
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
126 args = () |
|
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
127 if displayof: |
|
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
128 args = ('-displayof', displayof) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 if option: |
|
76115
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
130 args = args + ('-' + option, ) |
|
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
131 return self._call("font", "actual", self.name, *args) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 else: |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 return self._mkdict( |
|
76115
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
134 self._split(self._call("font", "actual", self.name, *args))) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 def cget(self, option): |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 "Get font attribute" |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 return self._call("font", "config", self.name, "-"+option) |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 def config(self, **options): |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 "Modify font attributes" |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 if options: |
|
28513
90c77b8af1a4
SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents:
16517
diff
changeset
|
143 self._call("font", "config", self.name, |
|
90c77b8af1a4
SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents:
16517
diff
changeset
|
144 *self._set(options)) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 else: |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 return self._mkdict( |
|
76088
db0063ce8a5c
reformat code to follow PEP8
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
64795
diff
changeset
|
147 self._split(self._call("font", "config", self.name))) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 configure = config |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
8960
diff
changeset
|
150 |
|
76115
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
151 def measure(self, text, displayof=None): |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 "Return text width" |
|
76115
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
153 args = (text,) |
|
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
154 if displayof: |
|
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
155 args = ('-displayof', displayof, text) |
|
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
156 return int(self._call("font", "measure", self.name, *args)) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 |
|
76115
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
158 def metrics(self, *options, **kw): |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 """Return font metrics. |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 For best performance, create a dummy widget |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 using this font before calling this method.""" |
|
76115
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
163 args = () |
|
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
164 displayof = kw.pop('displayof', None) |
|
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
165 if displayof: |
|
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
166 args = ('-displayof', displayof) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 if options: |
|
76115
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
168 args = args + self._get(options) |
|
16517
eee67a84e5fa
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15308
diff
changeset
|
169 return int( |
|
76115
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
170 self._call("font", "metrics", self.name, *args)) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 else: |
|
76115
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
172 res = self._split(self._call("font", "metrics", self.name, *args)) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 options = {} |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 for i in range(0, len(res), 2): |
|
16517
eee67a84e5fa
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15308
diff
changeset
|
175 options[res[i][1:]] = int(res[i+1]) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 return options |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 |
|
76088
db0063ce8a5c
reformat code to follow PEP8
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
64795
diff
changeset
|
178 |
|
76115
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
179 def families(root=None, displayof=None): |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 "Get font families (as a tuple)" |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 if not root: |
|
47130
ef4cca2d6171
Tkinter, step 2: adapt imports.
Georg Brandl <georg@python.org>
parents:
47129
diff
changeset
|
182 root = tkinter._default_root |
|
76115
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
183 args = () |
|
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
184 if displayof: |
|
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
185 args = ('-displayof', displayof) |
|
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
186 return root.tk.splitlist(root.tk.call("font", "families", *args)) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 |
|
76088
db0063ce8a5c
reformat code to follow PEP8
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
64795
diff
changeset
|
188 |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 def names(root=None): |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 "Get names of defined fonts (as a tuple)" |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 if not root: |
|
47130
ef4cca2d6171
Tkinter, step 2: adapt imports.
Georg Brandl <georg@python.org>
parents:
47129
diff
changeset
|
192 root = tkinter._default_root |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 return root.tk.splitlist(root.tk.call("font", "names")) |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 |
|
76088
db0063ce8a5c
reformat code to follow PEP8
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
64795
diff
changeset
|
195 |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 # -------------------------------------------------------------------- |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 # test stuff |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
8960
diff
changeset
|
198 |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 if __name__ == "__main__": |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 |
|
47130
ef4cca2d6171
Tkinter, step 2: adapt imports.
Georg Brandl <georg@python.org>
parents:
47129
diff
changeset
|
201 root = tkinter.Tk() |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 # create a font |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 f = Font(family="times", size=30, weight=NORMAL) |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
206 print(f.actual()) |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
207 print(f.actual("family")) |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
208 print(f.actual("weight")) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
210 print(f.config()) |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
211 print(f.cget("family")) |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
212 print(f.cget("weight")) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
214 print(names()) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
216 print(f.measure("hello"), f.metrics("linespace")) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 |
|
76115
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
218 print(f.metrics(displayof=root)) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 f = Font(font=("Courier", 20, "bold")) |
|
76115
774c2afa6665
Issue #3033: Add displayof parameter to tkinter font.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
76089
diff
changeset
|
221 print(f.measure("hello"), f.metrics("linespace", displayof=root)) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 |
|
47130
ef4cca2d6171
Tkinter, step 2: adapt imports.
Georg Brandl <georg@python.org>
parents:
47129
diff
changeset
|
223 w = tkinter.Label(root, text="Hello, world", font=f) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 w.pack() |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 |
|
47130
ef4cca2d6171
Tkinter, step 2: adapt imports.
Georg Brandl <georg@python.org>
parents:
47129
diff
changeset
|
226 w = tkinter.Button(root, text="Quit!", command=root.destroy) |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 w.pack() |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 fb = Font(font=w["font"]).copy() |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 fb.config(weight=BOLD) |
|
15308
847ae40074d4
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
8960
diff
changeset
|
231 |
|
8960
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 w.config(font=fb) |
|
9618a86879ca
Fredrik Lundh's font wrapper.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 |
|
47130
ef4cca2d6171
Tkinter, step 2: adapt imports.
Georg Brandl <georg@python.org>
parents:
47129
diff
changeset
|
234 tkinter.mainloop() |
