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 """Represents a named font.
31 Constructor options are:
33 font -- font specifier (name, system font, or (family, size, style)-tuple)
37 family -- font 'family', e.g. Courier, Times, Helvetica
38 size -- font size in points
39 weight -- font thickness: NORMAL, BOLD
40 slant -- font slant: NORMAL, ITALIC
41 underline -- font underlining: false (0), true (1)
42 overstrike -- font strikeout: false (0), true (1)
43 name -- name to use for this font configuration (defaults to a unique name)
48 for k
, v
in kw
.items():
50 options
.append(str(v
))
59 def _mkdict(self
, args
):
61 for i
in range(0, len(args
), 2):
62 options
[args
[i
][1:]] = args
[i
+1]
65 def __init__(self
, root
=None, font
=None, name
=None, **options
):
67 root
= Tkinter
._default
_root
69 # get actual settings corresponding to the given font
70 font
= root
.tk
.splitlist(root
.tk
.call("font", "actual", font
))
72 font
= self
._set
(options
)
74 name
= "font" + str(id(self
))
76 apply(root
.tk
.call
, ("font", "create", name
) + font
)
79 self
._split
= root
.tk
.splitlist
80 self
._call
= root
.tk
.call
87 self
._call
("font", "delete", self
.name
)
88 except (AttributeError, Tkinter
.TclError
):
92 "Return a distinct copy of the current font"
93 return apply(Font
, (self
._root
,), self
.actual())
95 def actual(self
, option
=None):
96 "Return actual font attributes"
98 return self
._call
("font", "actual", self
.name
, "-"+option
)
101 self
._split
(self
._call
("font", "actual", self
.name
))
104 def cget(self
, option
):
106 return self
._call
("font", "config", self
.name
, "-"+option
)
108 def config(self
, **options
):
109 "Modify font attributes"
111 apply(self
._call
, ("font", "config", self
.name
) +
115 self
._split
(self
._call
("font", "config", self
.name
))
120 def measure(self
, text
):
122 return int(self
._call
("font", "measure", self
.name
, text
))
124 def metrics(self
, *options
):
125 """Return font metrics.
127 For best performance, create a dummy widget
128 using this font before calling this method."""
132 self
._call
("font", "metrics", self
.name
, self
._get
(options
))
135 res
= self
._split
(self
._call
("font", "metrics", self
.name
))
137 for i
in range(0, len(res
), 2):
138 options
[res
[i
][1:]] = int(res
[i
+1])
141 def families(root
=None):
142 "Get font families (as a tuple)"
144 root
= Tkinter
._default
_root
145 return root
.tk
.splitlist(root
.tk
.call("font", "families"))
147 def names(root
=None):
148 "Get names of defined fonts (as a tuple)"
150 root
= Tkinter
._default
_root
151 return root
.tk
.splitlist(root
.tk
.call("font", "names"))
153 # --------------------------------------------------------------------
156 if __name__
== "__main__":
161 f
= Font(family
="times", size
=30, weight
=NORMAL
)
164 print f
.actual("family")
165 print f
.actual("weight")
168 print f
.cget("family")
169 print f
.cget("weight")
173 print f
.measure("hello"), f
.metrics("linespace")
177 f
= Font(font
=("Courier", 20, "bold"))
178 print f
.measure("hello"), f
.metrics("linespace")
180 w
= Tkinter
.Label(root
, text
="Hello, world", font
=f
)
183 w
= Tkinter
.Button(root
, text
="Quit!", command
=root
.destroy
)
186 fb
= Font(font
=w
["font"]).copy()
187 fb
.config(weight
=BOLD
)