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
30 """Represents a named font.
32 Constructor options are:
34 font -- font specifier (name, system font, or (family, size, style)-tuple)
38 family -- font 'family', e.g. Courier, Times, Helvetica
39 size -- font size in points
40 weight -- font thickness: NORMAL, BOLD
41 slant -- font slant: NORMAL, ITALIC
42 underline -- font underlining: false (0), true (1)
43 overstrike -- font strikeout: false (0), true (1)
44 name -- name to use for this font configuration (defaults to a unique name)
49 for k
, v
in kw
.items():
51 options
.append(str(v
))
60 def _mkdict(self
, args
):
62 for i
in range(0, len(args
), 2):
63 options
[args
[i
][1:]] = args
[i
+1]
66 def __init__(self
, root
=None, font
=None, name
=None, **options
):
68 root
= Tkinter
._default
_root
70 # get actual settings corresponding to the given font
71 font
= root
.tk
.splitlist(root
.tk
.call("font", "actual", font
))
73 font
= self
._set
(options
)
75 name
= "font" + str(id(self
))
77 apply(root
.tk
.call
, ("font", "create", name
) + font
)
80 self
._split
= root
.tk
.splitlist
81 self
._call
= root
.tk
.call
88 self
._call
("font", "delete", self
.name
)
89 except (AttributeError, Tkinter
.TclError
):
93 "Return a distinct copy of the current font"
94 return apply(Font
, (self
._root
,), self
.actual())
96 def actual(self
, option
=None):
97 "Return actual font attributes"
99 return self
._call
("font", "actual", self
.name
, "-"+option
)
102 self
._split
(self
._call
("font", "actual", self
.name
))
105 def cget(self
, option
):
107 return self
._call
("font", "config", self
.name
, "-"+option
)
109 def config(self
, **options
):
110 "Modify font attributes"
112 apply(self
._call
, ("font", "config", self
.name
) +
116 self
._split
(self
._call
("font", "config", self
.name
))
121 def measure(self
, text
):
123 return string
.atoi(self
._call
("font", "measure", self
.name
, text
))
125 def metrics(self
, *options
):
126 """Return font metrics.
128 For best performance, create a dummy widget
129 using this font before calling this method."""
133 self
._call
("font", "metrics", self
.name
, self
._get
(options
))
136 res
= self
._split
(self
._call
("font", "metrics", self
.name
))
138 for i
in range(0, len(res
), 2):
139 options
[res
[i
][1:]] = string
.atoi(res
[i
+1])
142 def families(root
=None):
143 "Get font families (as a tuple)"
145 root
= Tkinter
._default
_root
146 return root
.tk
.splitlist(root
.tk
.call("font", "families"))
148 def names(root
=None):
149 "Get names of defined fonts (as a tuple)"
151 root
= Tkinter
._default
_root
152 return root
.tk
.splitlist(root
.tk
.call("font", "names"))
154 # --------------------------------------------------------------------
157 if __name__
== "__main__":
162 f
= Font(family
="times", size
=30, weight
=NORMAL
)
165 print f
.actual("family")
166 print f
.actual("weight")
169 print f
.cget("family")
170 print f
.cget("weight")
174 print f
.measure("hello"), f
.metrics("linespace")
178 f
= Font(font
=("Courier", 20, "bold"))
179 print f
.measure("hello"), f
.metrics("linespace")
181 w
= Tkinter
.Label(root
, text
="Hello, world", font
=f
)
184 w
= Tkinter
.Button(root
, text
="Quit!", command
=root
.destroy
)
187 fb
= Font(font
=w
["font"]).copy()
188 fb
.config(weight
=BOLD
)