FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-143990: Preserve negative pixel sizes in tkinter.font.Font by dawdameet · Pull Request #143992 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
9 changes: 9 additions & 0 deletions Lib/test/test_tkinter/test_font.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ def test_iterable_protocol(self):
with self.assertRaisesRegex(TypeError, 'is not a container or iterable'):
self.font in self.font

def test_negative_pixel_size(self):
# gh-143990: negative sizes (pixels) should be preserved in Font objects
pixel_size = -14
f = font.Font(self.root, family='Helvetica', size=pixel_size)
self.assertEqual(f.cget("size"), pixel_size)
# Test initialization with font tuple
f2 = font.Font(self.root, font=('Helvetica', pixel_size))
self.assertEqual(f2.cget("size"), pixel_size)


class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase):

Expand Down
16 changes: 15 additions & 1 deletion Lib/tkinter/font.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,21 @@ def __init__(self, root=None, font=None, name=None, exists=False,
tk = getattr(root, 'tk', root)
if font:
# get actual settings corresponding to the given font
font = tk.splitlist(tk.call("font", "actual", font))
font_opts = tk.splitlist(tk.call("font", "actual", font))
#if input was passed as tuple, restore
# font actual will normalize negative (pixel) -> positive(points)
if isinstance(font, tuple) and len(font) >= 2:
# format=(family, size, styles)
req_size = font[1]
if isinstance(req_size, (int, float)):
opt=list(font_opts)
try:
i = opt.index('-size')
opt[i+1] = str(req_size)
font_opts = tuple(opt)
except ValueError:
pass
font = font_opts
else:
font = self._set(options)
if not name:
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Preserve negative font sizes (pixels) in :class:`tkinter.font.Font` when
defined via a tuple. Previously, these were incorrectly normalized to
positive sizes (points) by Tcl's ``font actual`` command.
Loading

Back | FazBrowse Home | New Git URL