Skip to content

gh-143990: Preserve the size when creating a Font from a named font#153267

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-143990-font-configure
Open

gh-143990: Preserve the size when creating a Font from a named font#153267
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-143990-font-configure

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

When a tkinter.font.Font is created from another font, Font.__init__ expanded it into concrete options with Tcl's font actual. For a named font this resolves the configured size, so a size specified in pixels (a negative size) was silently converted to a size in points (gh-143990).

font configure returns the configured options and preserves such a size, but it only accepts a font name, not a font description. So the font is now expanded with font configure when it is a named font, falling back to font actual when it is a font description:

try:
    font = tk.splitlist(tk.call("font", "configure", font))
except tkinter.TclError:
    font = tk.splitlist(tk.call("font", "actual", font))

Trying font configure first delegates the "is this a named font?" decision to Tcl, which is robust even for font names containing spaces.

A font description (a tuple or a string) is still resolved via font actual, since there is no way to parse it into options otherwise; wrapping it with exists=True (gh-143990, #152025) remains the way to use it without loss of precision.

This is the general form of #143992, which restored the size only for a size given in a tuple.

…font

tkinter.font.Font now copies the options of a named font (via "font
configure") instead of the options resolved by "font actual", which
would resolve a size specified in pixels (a negative size) to points.
A font description is still resolved, as it cannot be parsed otherwise.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant