Skip to content

Commit 9291b83

Browse files
committed
gh-143990: Preserve negative pixel sizes in tkinter.font.Font
1 parent 63cc125 commit 9291b83

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

Lib/test/test_tkinter/test_font.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ def test_iterable_protocol(self):
130130
with self.assertRaisesRegex(TypeError, 'is not a container or iterable'):
131131
self.font in self.font
132132

133+
def test_negative_pixel_size(self):
134+
# gh-143990: negative sizes (pixels) should be preserved in Font objects
135+
pixel_size = -14
136+
f = font.Font(self.root, family='Helvetica', size=pixel_size)
137+
self.assertEqual(f.cget("size"), pixel_size)
138+
139+
# Test initialization with font tuple
140+
f2 = font.Font(self.root, font=('Helvetica', pixel_size))
141+
self.assertEqual(f2.cget("size"), pixel_size)
142+
133143

134144
class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase):
135145

Lib/tkinter/font.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,21 @@ def __init__(self, root=None, font=None, name=None, exists=False,
7272
tk = getattr(root, 'tk', root)
7373
if font:
7474
# get actual settings corresponding to the given font
75-
font = tk.splitlist(tk.call("font", "actual", font))
75+
font_opts = tk.splitlist(tk.call("font", "actual", font))
76+
#if input was passed as tuple, restore
77+
# font actual will normalize negative (pixel) -> positive(points)
78+
if isinstance(font, tuple) and len(font) >= 2:
79+
# format=(family, size, styles)
80+
req_size = font[1]
81+
if isinstance(req_size, (int, float)):
82+
opt=list(font_opts)
83+
try:
84+
i = opt.index('-size')
85+
opt[i+1] = str(req_size)
86+
font_opts = tuple(opt)
87+
except ValueError:
88+
pass
89+
font = font_opts
7690
else:
7791
font = self._set(options)
7892
if not name:

0 commit comments

Comments
 (0)