Index: Lib/lib-tk/tkFont.py =================================================================== --- Lib/lib-tk/tkFont.py (revision 63916) +++ Lib/lib-tk/tkFont.py (working copy) @@ -2,9 +2,6 @@ # # written by Fredrik Lundh, February 1998 # -# FIXME: should add 'displayof' option where relevant (actual, families, -# measure, and metrics) -# __version__ = "0.9" @@ -17,12 +14,10 @@ ITALIC = "italic" def nametofont(name): - """Given the name of a tk named font, returns a Font representation. - """ + """Given the name of a tk named font, returns a Font representation.""" return Font(name=name, exists=True) class Font: - """Represents a named font. Constructor options are: @@ -41,7 +36,6 @@ slant -- font slant: ROMAN, ITALIC underline -- font underlining: false (0), true (1) overstrike -- font strikeout: false (0), true (1) - """ def _set(self, kw): @@ -117,14 +111,18 @@ "Return a distinct copy of the current font" return Font(self._root, **self.actual()) - def actual(self, option=None): + def actual(self, option=None, displayof=None): "Return actual font attributes" + args = () + if displayof: + args = ('-displayof', displayof) + if option: - return self._call("font", "actual", self.name, "-"+option) + args = args + ('-' + option, ) + return self._call("font", "actual", self.name, *args) else: return self._mkdict( - self._split(self._call("font", "actual", self.name)) - ) + self._split(self._call("font", "actual", self.name, *args))) def cget(self, option): "Get font attribute" @@ -142,37 +140,49 @@ configure = config - def measure(self, text): + def measure(self, text, displayof=None): "Return text width" - return int(self._call("font", "measure", self.name, text)) + args = (text, ) + if displayof: + args = ('-displayof', displayof, text) - def metrics(self, *options): + return int(self._call("font", "measure", self.name, *args)) + + def metrics(self, *options, **kw): """Return font metrics. For best performance, create a dummy widget using this font before calling this method.""" + args = () + displayof = kw.pop('displayof', None) + if displayof: + args = ('-displayof', displayof) if options: - return int( - self._call("font", "metrics", self.name, self._get(options)) - ) + args = args + self._get(options) + return int(self._call("font", "metrics", self.name, *args)) else: - res = self._split(self._call("font", "metrics", self.name)) + res = self._split(self._call("font", "metrics", self.name, *args)) options = {} for i in range(0, len(res), 2): options[res[i][1:]] = int(res[i+1]) return options -def families(root=None): +def families(root=None, displayof=None): "Get font families (as a tuple)" if not root: root = Tkinter._default_root - return root.tk.splitlist(root.tk.call("font", "families")) + args = () + if displayof: + args = ('-displayof', displayof) + return root.tk.splitlist(root.tk.call("font", "families", *args)) + def names(root=None): "Get names of defined fonts (as a tuple)" if not root: root = Tkinter._default_root + return root.tk.splitlist(root.tk.call("font", "names")) # -------------------------------------------------------------------- @@ -197,10 +207,10 @@ print f.measure("hello"), f.metrics("linespace") - print f.metrics() + print f.metrics(displayof=root) f = Font(font=("Courier", 20, "bold")) - print f.measure("hello"), f.metrics("linespace") + print f.measure("hello"), f.metrics("linespace", displayof=root) w = Tkinter.Label(root, text="Hello, world", font=f) w.pack()