gh-143990: Preserve negative pixel sizes in tkinter.font.Font#143992
Open
dawdameet wants to merge 1 commit into
Open
gh-143990: Preserve negative pixel sizes in tkinter.font.Font#143992dawdameet wants to merge 1 commit into
dawdameet wants to merge 1 commit into
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
9291b83 to
88bc475
Compare
88bc475 to
738f39b
Compare
|
Thank you. I wasn't sure where to comment this, but I found a case where the error wasn't fixed. import tkinter as tk
import tkinter.font as tkfont
root = tk.Tk()
font_tuple = ("Calibri", -14)
# font = tkfont.Font(root, font_tuple)
lb1 = tk.Label(root, text="Hello", font=font_tuple)
lb1.pack()
font = tkfont.Font(root, lb1["font"]) #
lb2 = tk.Label(root, text="Hello", font=font)
lb2.pack()
root.update()
print(lb1.winfo_width(), lb1.winfo_height())
print(lb2.winfo_width(), lb2.winfo_height())
root.mainloop() |
|
This PR is stale because it has been open for 30 days with no activity. |
Member
|
#152025 is an alternative solution which allows to wrap a font description, not only a named font. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.