Skip to content

gh-143990: Preserve negative pixel sizes in tkinter.font.Font#143992

Open
dawdameet wants to merge 1 commit into
python:mainfrom
dawdameet:fix-issue-143990-tkinter-font
Open

gh-143990: Preserve negative pixel sizes in tkinter.font.Font#143992
dawdameet wants to merge 1 commit into
python:mainfrom
dawdameet:fix-issue-143990-tkinter-font

Conversation

@dawdameet

Copy link
Copy Markdown

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.

@bedevere-app

bedevere-app Bot commented Jan 18, 2026

Copy link
Copy Markdown

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 skip news label instead.

@dawdameet dawdameet force-pushed the fix-issue-143990-tkinter-font branch from 88bc475 to 738f39b Compare January 18, 2026 10:11
@Kawasaki5963

Copy link
Copy Markdown

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()

@serhiy-storchaka serhiy-storchaka self-requested a review January 20, 2026 09:16
@github-actions

github-actions Bot commented May 5, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 5, 2026
@serhiy-storchaka

Copy link
Copy Markdown
Member

#152025 is an alternative solution which allows to wrap a font description, not only a named font.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants