Skip to content

Conversation

@dawdameet
Copy link

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
Copy link

bedevere-app bot commented Jan 18, 2026

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

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants