openmedialibrary_platform/Darwin/lib/python3.5/tkinter/font.py

233 lines
6.4 KiB
Python
Raw Normal View History

2013-10-11 17:28:32 +00:00
# Tkinter font wrapper
#
# written by Fredrik Lundh, February 1998
#
__version__ = "0.9"
2014-09-30 16:15:32 +00:00
import itertools
import tkinter
2013-10-11 17:28:32 +00:00
# weight/slant
NORMAL = "normal"
ROMAN = "roman"
BOLD = "bold"
ITALIC = "italic"
2014-09-30 16:15:32 +00:00
2013-10-11 17:28:32 +00:00
def nametofont(name):
"""Given the name of a tk named font, returns a Font representation.
"""
return Font(name=name, exists=True)
2014-09-30 16:15:32 +00:00
class Font:
2013-10-11 17:28:32 +00:00
"""Represents a named font.
Constructor options are:
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.
2014-09-30 16:15:32 +00:00
Raises _tkinter.TclError if the assertion is false.
2013-10-11 17:28:32 +00:00
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)
"""
2014-09-30 16:15:32 +00:00
counter = itertools.count(1)
2013-10-11 17:28:32 +00:00
def _set(self, kw):
options = []
for k, v in kw.items():
options.append("-"+k)
options.append(str(v))
return tuple(options)
def _get(self, args):
options = []
for k in args:
options.append("-"+k)
return tuple(options)
def _mkdict(self, args):
options = {}
for i in range(0, len(args), 2):
options[args[i][1:]] = args[i+1]
return options
2014-09-30 16:15:32 +00:00
def __init__(self, root=None, font=None, name=None, exists=False,
**options):
2013-10-11 17:28:32 +00:00
if not root:
2014-09-30 16:15:32 +00:00
root = tkinter._default_root
2015-11-25 01:25:01 +00:00
tk = getattr(root, 'tk', root)
2013-10-11 17:28:32 +00:00
if font:
# get actual settings corresponding to the given font
2015-11-25 01:25:01 +00:00
font = tk.splitlist(tk.call("font", "actual", font))
2013-10-11 17:28:32 +00:00
else:
font = self._set(options)
if not name:
2014-09-30 16:15:32 +00:00
name = "font" + str(next(self.counter))
2013-10-11 17:28:32 +00:00
self.name = name
if exists:
self.delete_font = False
# confirm font exists
2015-11-25 01:25:01 +00:00
if self.name not in tk.splitlist(tk.call("font", "names")):
2014-09-30 16:15:32 +00:00
raise tkinter._tkinter.TclError(
"named font %s does not already exist" % (self.name,))
2013-10-11 17:28:32 +00:00
# if font config info supplied, apply it
if font:
2015-11-25 01:25:01 +00:00
tk.call("font", "configure", self.name, *font)
2013-10-11 17:28:32 +00:00
else:
# create new font (raises TclError if the font exists)
2015-11-25 01:25:01 +00:00
tk.call("font", "create", self.name, *font)
2013-10-11 17:28:32 +00:00
self.delete_font = True
2015-11-25 01:25:01 +00:00
self._tk = tk
self._split = tk.splitlist
self._call = tk.call
2013-10-11 17:28:32 +00:00
def __str__(self):
return self.name
def __eq__(self, other):
2014-09-30 16:15:32 +00:00
return isinstance(other, Font) and self.name == other.name
2013-10-11 17:28:32 +00:00
def __getitem__(self, key):
return self.cget(key)
def __setitem__(self, key, value):
self.configure(**{key: value})
def __del__(self):
try:
if self.delete_font:
self._call("font", "delete", self.name)
except Exception:
pass
def copy(self):
"Return a distinct copy of the current font"
2015-11-25 01:25:01 +00:00
return Font(self._tk, **self.actual())
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
def actual(self, option=None, displayof=None):
2013-10-11 17:28:32 +00:00
"Return actual font attributes"
2014-09-30 16:15:32 +00:00
args = ()
if displayof:
args = ('-displayof', displayof)
2013-10-11 17:28:32 +00:00
if option:
2014-09-30 16:15:32 +00:00
args = args + ('-' + option, )
return self._call("font", "actual", self.name, *args)
2013-10-11 17:28:32 +00:00
else:
return self._mkdict(
2014-09-30 16:15:32 +00:00
self._split(self._call("font", "actual", self.name, *args)))
2013-10-11 17:28:32 +00:00
def cget(self, option):
"Get font attribute"
return self._call("font", "config", self.name, "-"+option)
def config(self, **options):
"Modify font attributes"
if options:
self._call("font", "config", self.name,
*self._set(options))
else:
return self._mkdict(
2014-09-30 16:15:32 +00:00
self._split(self._call("font", "config", self.name)))
2013-10-11 17:28:32 +00:00
configure = config
2014-09-30 16:15:32 +00:00
def measure(self, text, displayof=None):
2013-10-11 17:28:32 +00:00
"Return text width"
2014-09-30 16:15:32 +00:00
args = (text,)
if displayof:
args = ('-displayof', displayof, text)
2015-11-25 01:25:01 +00:00
return self._tk.getint(self._call("font", "measure", self.name, *args))
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
def metrics(self, *options, **kw):
2013-10-11 17:28:32 +00:00
"""Return font metrics.
For best performance, create a dummy widget
using this font before calling this method."""
2014-09-30 16:15:32 +00:00
args = ()
displayof = kw.pop('displayof', None)
if displayof:
args = ('-displayof', displayof)
2013-10-11 17:28:32 +00:00
if options:
2014-09-30 16:15:32 +00:00
args = args + self._get(options)
2015-11-25 01:25:01 +00:00
return self._tk.getint(
2014-09-30 16:15:32 +00:00
self._call("font", "metrics", self.name, *args))
2013-10-11 17:28:32 +00:00
else:
2014-09-30 16:15:32 +00:00
res = self._split(self._call("font", "metrics", self.name, *args))
2013-10-11 17:28:32 +00:00
options = {}
for i in range(0, len(res), 2):
2015-11-25 01:25:01 +00:00
options[res[i][1:]] = self._tk.getint(res[i+1])
2013-10-11 17:28:32 +00:00
return options
2014-09-30 16:15:32 +00:00
def families(root=None, displayof=None):
2013-10-11 17:28:32 +00:00
"Get font families (as a tuple)"
if not root:
2014-09-30 16:15:32 +00:00
root = tkinter._default_root
args = ()
if displayof:
args = ('-displayof', displayof)
return root.tk.splitlist(root.tk.call("font", "families", *args))
2013-10-11 17:28:32 +00:00
def names(root=None):
"Get names of defined fonts (as a tuple)"
if not root:
2014-09-30 16:15:32 +00:00
root = tkinter._default_root
2013-10-11 17:28:32 +00:00
return root.tk.splitlist(root.tk.call("font", "names"))
2014-09-30 16:15:32 +00:00
2013-10-11 17:28:32 +00:00
# --------------------------------------------------------------------
# test stuff
if __name__ == "__main__":
2014-09-30 16:15:32 +00:00
root = tkinter.Tk()
2013-10-11 17:28:32 +00:00
# create a font
f = Font(family="times", size=30, weight=NORMAL)
2014-09-30 16:15:32 +00:00
print(f.actual())
print(f.actual("family"))
print(f.actual("weight"))
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
print(f.config())
print(f.cget("family"))
print(f.cget("weight"))
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
print(names())
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
print(f.measure("hello"), f.metrics("linespace"))
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
print(f.metrics(displayof=root))
2013-10-11 17:28:32 +00:00
f = Font(font=("Courier", 20, "bold"))
2014-09-30 16:15:32 +00:00
print(f.measure("hello"), f.metrics("linespace", displayof=root))
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
w = tkinter.Label(root, text="Hello, world", font=f)
2013-10-11 17:28:32 +00:00
w.pack()
2014-09-30 16:15:32 +00:00
w = tkinter.Button(root, text="Quit!", command=root.destroy)
2013-10-11 17:28:32 +00:00
w.pack()
fb = Font(font=w["font"]).copy()
fb.config(weight=BOLD)
w.config(font=fb)
2014-09-30 16:15:32 +00:00
tkinter.mainloop()