gh-143990: Preserve negative pixel sizes in tkinter.font.Font #143992
+27
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
gh-143990: Preserve negative pixel sizes in tkinter.font.Font
Summary
This PR fixes a bug where tkinter.font.Font objects would lose their negative sign (representing pixel size) and convert it to a positive integer (representing points) during initialization.
details
The Font.init method calls tk.call("font", "actual", font). In Tcl/Tk, font actual resolves the requested font to the system's closest match. If a font is requested in pixels (e.g., -14), Tcl calculates the equivalent point size (e.g., 11) based on the current DPI.
The current implementation in tkinter/font.py was blindly accepting this resolved value and overwriting the user's original request. This fix checks if the input was a tuple containing a size, and if so, restores that specific size after the font actual call.
Changes
Modified Lib/tkinter/font.py to preserve the user-requested size.
Added a regression test test_negative_pixel_size to Lib/test/test_tkinter/test_font.py.
Verified that Font.cget("size") now returns the correct negative integer.