| 1 | n/a | # Tkinter font wrapper |
|---|
| 2 | n/a | # |
|---|
| 3 | n/a | # written by Fredrik Lundh, February 1998 |
|---|
| 4 | n/a | # |
|---|
| 5 | n/a | # FIXME: should add 'displayof' option where relevant (actual, families, |
|---|
| 6 | n/a | # measure, and metrics) |
|---|
| 7 | n/a | # |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | __version__ = "0.9" |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | import Tkinter |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | # weight/slant |
|---|
| 14 | n/a | NORMAL = "normal" |
|---|
| 15 | n/a | ROMAN = "roman" |
|---|
| 16 | n/a | BOLD = "bold" |
|---|
| 17 | n/a | ITALIC = "italic" |
|---|
| 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 | class Font: |
|---|
| 25 | n/a | |
|---|
| 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 | def _set(self, kw): |
|---|
| 48 | n/a | options = [] |
|---|
| 49 | n/a | for k, v in kw.items(): |
|---|
| 50 | n/a | options.append("-"+k) |
|---|
| 51 | n/a | options.append(str(v)) |
|---|
| 52 | n/a | return tuple(options) |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | def _get(self, args): |
|---|
| 55 | n/a | options = [] |
|---|
| 56 | n/a | for k in args: |
|---|
| 57 | n/a | options.append("-"+k) |
|---|
| 58 | n/a | return tuple(options) |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | def _mkdict(self, args): |
|---|
| 61 | n/a | options = {} |
|---|
| 62 | n/a | for i in range(0, len(args), 2): |
|---|
| 63 | n/a | options[args[i][1:]] = args[i+1] |
|---|
| 64 | n/a | return options |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | def __init__(self, root=None, font=None, name=None, exists=False, **options): |
|---|
| 67 | n/a | if not root: |
|---|
| 68 | n/a | root = Tkinter._default_root |
|---|
| 69 | n/a | if font: |
|---|
| 70 | n/a | # get actual settings corresponding to the given font |
|---|
| 71 | n/a | font = root.tk.splitlist(root.tk.call("font", "actual", font)) |
|---|
| 72 | n/a | else: |
|---|
| 73 | n/a | font = self._set(options) |
|---|
| 74 | n/a | if not name: |
|---|
| 75 | n/a | name = "font" + str(id(self)) |
|---|
| 76 | n/a | self.name = name |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | if exists: |
|---|
| 79 | n/a | self.delete_font = False |
|---|
| 80 | n/a | # confirm font exists |
|---|
| 81 | n/a | if self.name not in root.tk.call("font", "names"): |
|---|
| 82 | n/a | raise Tkinter._tkinter.TclError, "named font %s does not already exist" % (self.name,) |
|---|
| 83 | n/a | # if font config info supplied, apply it |
|---|
| 84 | n/a | if font: |
|---|
| 85 | n/a | root.tk.call("font", "configure", self.name, *font) |
|---|
| 86 | n/a | else: |
|---|
| 87 | n/a | # create new font (raises TclError if the font exists) |
|---|
| 88 | n/a | root.tk.call("font", "create", self.name, *font) |
|---|
| 89 | n/a | self.delete_font = True |
|---|
| 90 | n/a | # backlinks! |
|---|
| 91 | n/a | self._root = root |
|---|
| 92 | n/a | self._split = root.tk.splitlist |
|---|
| 93 | n/a | self._call = root.tk.call |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | def __str__(self): |
|---|
| 96 | n/a | return self.name |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | def __eq__(self, other): |
|---|
| 99 | n/a | return self.name == other.name and isinstance(other, Font) |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | def __getitem__(self, key): |
|---|
| 102 | n/a | return self.cget(key) |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | def __setitem__(self, key, value): |
|---|
| 105 | n/a | self.configure(**{key: value}) |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | def __del__(self): |
|---|
| 108 | n/a | try: |
|---|
| 109 | n/a | if self.delete_font: |
|---|
| 110 | n/a | self._call("font", "delete", self.name) |
|---|
| 111 | n/a | except (KeyboardInterrupt, SystemExit): |
|---|
| 112 | n/a | raise |
|---|
| 113 | n/a | except Exception: |
|---|
| 114 | n/a | pass |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | def copy(self): |
|---|
| 117 | n/a | "Return a distinct copy of the current font" |
|---|
| 118 | n/a | return Font(self._root, **self.actual()) |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | def actual(self, option=None): |
|---|
| 121 | n/a | "Return actual font attributes" |
|---|
| 122 | n/a | if option: |
|---|
| 123 | n/a | return self._call("font", "actual", self.name, "-"+option) |
|---|
| 124 | n/a | else: |
|---|
| 125 | n/a | return self._mkdict( |
|---|
| 126 | n/a | self._split(self._call("font", "actual", self.name)) |
|---|
| 127 | n/a | ) |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | def cget(self, option): |
|---|
| 130 | n/a | "Get font attribute" |
|---|
| 131 | n/a | return self._call("font", "config", self.name, "-"+option) |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | def config(self, **options): |
|---|
| 134 | n/a | "Modify font attributes" |
|---|
| 135 | n/a | if options: |
|---|
| 136 | n/a | self._call("font", "config", self.name, |
|---|
| 137 | n/a | *self._set(options)) |
|---|
| 138 | n/a | else: |
|---|
| 139 | n/a | return self._mkdict( |
|---|
| 140 | n/a | self._split(self._call("font", "config", self.name)) |
|---|
| 141 | n/a | ) |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | configure = config |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | def measure(self, text): |
|---|
| 146 | n/a | "Return text width" |
|---|
| 147 | n/a | return int(self._call("font", "measure", self.name, text)) |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | def metrics(self, *options): |
|---|
| 150 | n/a | """Return font metrics. |
|---|
| 151 | n/a | |
|---|
| 152 | n/a | For best performance, create a dummy widget |
|---|
| 153 | n/a | using this font before calling this method.""" |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | if options: |
|---|
| 156 | n/a | return int( |
|---|
| 157 | n/a | self._call("font", "metrics", self.name, self._get(options)) |
|---|
| 158 | n/a | ) |
|---|
| 159 | n/a | else: |
|---|
| 160 | n/a | res = self._split(self._call("font", "metrics", self.name)) |
|---|
| 161 | n/a | options = {} |
|---|
| 162 | n/a | for i in range(0, len(res), 2): |
|---|
| 163 | n/a | options[res[i][1:]] = int(res[i+1]) |
|---|
| 164 | n/a | return options |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | def families(root=None): |
|---|
| 167 | n/a | "Get font families (as a tuple)" |
|---|
| 168 | n/a | if not root: |
|---|
| 169 | n/a | root = Tkinter._default_root |
|---|
| 170 | n/a | return root.tk.splitlist(root.tk.call("font", "families")) |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | def names(root=None): |
|---|
| 173 | n/a | "Get names of defined fonts (as a tuple)" |
|---|
| 174 | n/a | if not root: |
|---|
| 175 | n/a | root = Tkinter._default_root |
|---|
| 176 | n/a | return root.tk.splitlist(root.tk.call("font", "names")) |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | # -------------------------------------------------------------------- |
|---|
| 179 | n/a | # test stuff |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | if __name__ == "__main__": |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | root = Tkinter.Tk() |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | # create a font |
|---|
| 186 | n/a | f = Font(family="times", size=30, weight=NORMAL) |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | print f.actual() |
|---|
| 189 | n/a | print f.actual("family") |
|---|
| 190 | n/a | print f.actual("weight") |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | print f.config() |
|---|
| 193 | n/a | print f.cget("family") |
|---|
| 194 | n/a | print f.cget("weight") |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | print names() |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | print f.measure("hello"), f.metrics("linespace") |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | print f.metrics() |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | f = Font(font=("Courier", 20, "bold")) |
|---|
| 203 | n/a | print f.measure("hello"), f.metrics("linespace") |
|---|
| 204 | n/a | |
|---|
| 205 | n/a | w = Tkinter.Label(root, text="Hello, world", font=f) |
|---|
| 206 | n/a | w.pack() |
|---|
| 207 | n/a | |
|---|
| 208 | n/a | w = Tkinter.Button(root, text="Quit!", command=root.destroy) |
|---|
| 209 | n/a | w.pack() |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | fb = Font(font=w["font"]).copy() |
|---|
| 212 | n/a | fb.config(weight=BOLD) |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | w.config(font=fb) |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | Tkinter.mainloop() |
|---|