# $Id: tkFont.py,v 1.9 2004/08/20 06:19:23 loewis Exp $
# written by Fredrik Lundh <fredrik@pythonware.com>, February 1998
# FIXME: should add 'displayof' option where relevant (actual, families,
# Copyright (c) Secret Labs AB 1998.
# http://www.pythonware.com
"""Given the name of a tk named font, returns a Font representation.
return Font(name
=name
, exists
=True)
"""Represents a named font.
font -- font specifier (name, system font, or (family, size, style)-tuple)
name -- name to use for this font configuration (defaults to a unique name)
exists -- does a named font by this name already exist?
Creates a new named font if False, points to the existing font if True.
Raises _tkinter.TclError if the assertion is false.
the following are ignored if font is specified:
family -- font 'family', e.g. Courier, Times, Helvetica
size -- font size in points
weight -- font thickness: NORMAL, BOLD
slant -- font slant: ROMAN, ITALIC
underline -- font underlining: false (0), true (1)
overstrike -- font strikeout: false (0), true (1)
for i
in range(0, len(args
), 2):
options
[args
[i
][1:]] = args
[i
+1]
def __init__(self
, root
=None, font
=None, name
=None, exists
=False, **options
):
root
= Tkinter
._default
_root
# get actual settings corresponding to the given font
font
= root
.tk
.splitlist(root
.tk
.call("font", "actual", font
))
font
= self
._set
(options
)
name
= "font" + str(id(self
))
if self
.name
not in root
.tk
.call("font", "names"):
raise Tkinter
._tkinter
.TclError
, "named font %s does not already exist" % (self
.name
,)
# if font config info supplied, apply it
root
.tk
.call("font", "configure", self
.name
, *font
)
# create new font (raises TclError if the font exists)
root
.tk
.call("font", "create", self
.name
, *font
)
self
._split
= root
.tk
.splitlist
self
._call
= root
.tk
.call
return self
.name
== other
.name
and isinstance(other
, Font
)
def __getitem__(self
, key
):
def __setitem__(self
, key
, value
):
self
.configure(**{key
: value
})
self
._call
("font", "delete", self
.name
)
except (AttributeError, Tkinter
.TclError
):
"Return a distinct copy of the current font"
return Font(self
._root
, **self
.actual())
def actual(self
, option
=None):
"Return actual font attributes"
return self
._call
("font", "actual", self
.name
, "-"+option
)
self
._split
(self
._call
("font", "actual", self
.name
))
return self
._call
("font", "config", self
.name
, "-"+option
)
def config(self
, **options
):
self
._call
("font", "config", self
.name
,
self
._split
(self
._call
("font", "config", self
.name
))
return int(self
._call
("font", "measure", self
.name
, text
))
def metrics(self
, *options
):
For best performance, create a dummy widget
using this font before calling this method."""
self
._call
("font", "metrics", self
.name
, self
._get
(options
))
res
= self
._split
(self
._call
("font", "metrics", self
.name
))
for i
in range(0, len(res
), 2):
options
[res
[i
][1:]] = int(res
[i
+1])
"Get font families (as a tuple)"
root
= Tkinter
._default
_root
return root
.tk
.splitlist(root
.tk
.call("font", "families"))
"Get names of defined fonts (as a tuple)"
root
= Tkinter
._default
_root
return root
.tk
.splitlist(root
.tk
.call("font", "names"))
# --------------------------------------------------------------------
if __name__
== "__main__":
f
= Font(family
="times", size
=30, weight
=NORMAL
)
print f
.measure("hello"), f
.metrics("linespace")
f
= Font(font
=("Courier", 20, "bold"))
print f
.measure("hello"), f
.metrics("linespace")
w
= Tkinter
.Label(root
, text
="Hello, world", font
=f
)
w
= Tkinter
.Button(root
, text
="Quit!", command
=root
.destroy
)
fb
= Font(font
=w
["font"]).copy()