3
# $Id: tkFont.py,v 1.9 2004/08/20 06:19:23 loewis Exp $
7
# written by Fredrik Lundh <fredrik@pythonware.com>, February 1998
9
# FIXME: should add 'displayof' option where relevant (actual, families,
10
# measure, and metrics)
12
# Copyright (c) Secret Labs AB 1998.
15
# http://www.pythonware.com
29
"""Given the name of a tk named font, returns a Font representation.
31
return Font(name=name, exists=True)
35
"""Represents a named font.
37
Constructor options are:
39
font -- font specifier (name, system font, or (family, size, style)-tuple)
40
name -- name to use for this font configuration (defaults to a unique name)
41
exists -- does a named font by this name already exist?
42
Creates a new named font if False, points to the existing font if True.
43
Raises _tkinter.TclError if the assertion is false.
45
the following are ignored if font is specified:
47
family -- font 'family', e.g. Courier, Times, Helvetica
48
size -- font size in points
49
weight -- font thickness: NORMAL, BOLD
50
slant -- font slant: ROMAN, ITALIC
51
underline -- font underlining: false (0), true (1)
52
overstrike -- font strikeout: false (0), true (1)
58
for k, v in kw.items():
60
options.append(str(v))
69
def _mkdict(self, args):
71
for i in range(0, len(args), 2):
72
options[args[i][1:]] = args[i+1]
75
def __init__(self, root=None, font=None, name=None, exists=False, **options):
77
root = Tkinter._default_root
79
# get actual settings corresponding to the given font
80
font = root.tk.splitlist(root.tk.call("font", "actual", font))
82
font = self._set(options)
84
name = "font" + str(id(self))
88
self.delete_font = False
90
if self.name not in root.tk.call("font", "names"):
91
raise Tkinter._tkinter.TclError, "named font %s does not already exist" % (self.name,)
92
# if font config info supplied, apply it
94
root.tk.call("font", "configure", self.name, *font)
96
# create new font (raises TclError if the font exists)
97
root.tk.call("font", "create", self.name, *font)
98
self.delete_font = True
101
self._split = root.tk.splitlist
102
self._call = root.tk.call
107
def __eq__(self, other):
108
return self.name == other.name and isinstance(other, Font)
110
def __getitem__(self, key):
111
return self.cget(key)
113
def __setitem__(self, key, value):
114
self.configure(**{key: value})
119
self._call("font", "delete", self.name)
120
except (AttributeError, Tkinter.TclError):
124
"Return a distinct copy of the current font"
125
return Font(self._root, **self.actual())
127
def actual(self, option=None):
128
"Return actual font attributes"
130
return self._call("font", "actual", self.name, "-"+option)
133
self._split(self._call("font", "actual", self.name))
136
def cget(self, option):
138
return self._call("font", "config", self.name, "-"+option)
140
def config(self, **options):
141
"Modify font attributes"
143
self._call("font", "config", self.name,
147
self._split(self._call("font", "config", self.name))
152
def measure(self, text):
154
return int(self._call("font", "measure", self.name, text))
156
def metrics(self, *options):
157
"""Return font metrics.
159
For best performance, create a dummy widget
160
using this font before calling this method."""
164
self._call("font", "metrics", self.name, self._get(options))
167
res = self._split(self._call("font", "metrics", self.name))
169
for i in range(0, len(res), 2):
170
options[res[i][1:]] = int(res[i+1])
173
def families(root=None):
174
"Get font families (as a tuple)"
176
root = Tkinter._default_root
177
return root.tk.splitlist(root.tk.call("font", "families"))
179
def names(root=None):
180
"Get names of defined fonts (as a tuple)"
182
root = Tkinter._default_root
183
return root.tk.splitlist(root.tk.call("font", "names"))
185
# --------------------------------------------------------------------
188
if __name__ == "__main__":
193
f = Font(family="times", size=30, weight=NORMAL)
196
print f.actual("family")
197
print f.actual("weight")
200
print f.cget("family")
201
print f.cget("weight")
205
print f.measure("hello"), f.metrics("linespace")
209
f = Font(font=("Courier", 20, "bold"))
210
print f.measure("hello"), f.metrics("linespace")
212
w = Tkinter.Label(root, text="Hello, world", font=f)
215
w = Tkinter.Button(root, text="Quit!", command=root.destroy)
218
fb = Font(font=w["font"]).copy()
219
fb.config(weight=BOLD)