Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 20bbf54

Browse files
committed
Issue #3033: Add displayof parameter to tkinter font.
Patch by Guilherme Polo.
1 parent 9a13b43 commit 20bbf54

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

Lib/tkinter/font.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
#
33
# written by Fredrik Lundh, February 1998
44
#
5-
# FIXME: should add 'displayof' option where relevant (actual, families,
6-
# measure, and metrics)
7-
#
85

96
__version__ = "0.9"
107

@@ -124,14 +121,17 @@ def copy(self):
124121
"Return a distinct copy of the current font"
125122
return Font(self._root, **self.actual())
126123

127-
def actual(self, option=None):
124+
def actual(self, option=None, displayof=None):
128125
"Return actual font attributes"
126+
args = ()
127+
if displayof:
128+
args = ('-displayof', displayof)
129129
if option:
130-
return self._call("font", "actual", self.name, "-"+option)
130+
args = args + ('-' + option, )
131+
return self._call("font", "actual", self.name, *args)
131132
else:
132133
return self._mkdict(
133-
self._split(self._call("font", "actual", self.name))
134-
)
134+
self._split(self._call("font", "actual", self.name, *args)))
135135

136136
def cget(self, option):
137137
"Get font attribute"
@@ -148,32 +148,42 @@ def config(self, **options):
148148

149149
configure = config
150150

151-
def measure(self, text):
151+
def measure(self, text, displayof=None):
152152
"Return text width"
153-
return int(self._call("font", "measure", self.name, text))
153+
args = (text,)
154+
if displayof:
155+
args = ('-displayof', displayof, text)
156+
return int(self._call("font", "measure", self.name, *args))
154157

155-
def metrics(self, *options):
158+
def metrics(self, *options, **kw):
156159
"""Return font metrics.
157160
158161
For best performance, create a dummy widget
159162
using this font before calling this method."""
160-
163+
args = ()
164+
displayof = kw.pop('displayof', None)
165+
if displayof:
166+
args = ('-displayof', displayof)
161167
if options:
168+
args = args + self._get(options)
162169
return int(
163-
self._call("font", "metrics", self.name, self._get(options)))
170+
self._call("font", "metrics", self.name, *args))
164171
else:
165-
res = self._split(self._call("font", "metrics", self.name))
172+
res = self._split(self._call("font", "metrics", self.name, *args))
166173
options = {}
167174
for i in range(0, len(res), 2):
168175
options[res[i][1:]] = int(res[i+1])
169176
return options
170177

171178

172-
def families(root=None):
179+
def families(root=None, displayof=None):
173180
"Get font families (as a tuple)"
174181
if not root:
175182
root = tkinter._default_root
176-
return root.tk.splitlist(root.tk.call("font", "families"))
183+
args = ()
184+
if displayof:
185+
args = ('-displayof', displayof)
186+
return root.tk.splitlist(root.tk.call("font", "families", *args))
177187

178188

179189
def names(root=None):
@@ -205,10 +215,10 @@ def names(root=None):
205215

206216
print(f.measure("hello"), f.metrics("linespace"))
207217

208-
print(f.metrics())
218+
print(f.metrics(displayof=root))
209219

210220
f = Font(font=("Courier", 20, "bold"))
211-
print(f.measure("hello"), f.metrics("linespace"))
221+
print(f.measure("hello"), f.metrics("linespace", displayof=root))
212222

213223
w = tkinter.Label(root, text="Hello, world", font=f)
214224
w.pack()

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Core and Builtins
1919
Library
2020
-------
2121

22+
- Issue #3033: Add displayof parameter to tkinter font. Patch by Guilherme Polo.
23+
2224
- Issue #14482: Raise a ValueError, not a NameError, when trying to create
2325
a multiprocessing Client or Listener with an AF_UNIX type address under
2426
Windows. Patch by Popa Claudiu.

0 commit comments

Comments
 (0)