Skip to content

gh-143990: Allow tkinter.font.Font to wrap a font description#152025

Merged
serhiy-storchaka merged 4 commits into
python:mainfrom
serhiy-storchaka:tkinter-font-wrap-description
Jul 7, 2026
Merged

gh-143990: Allow tkinter.font.Font to wrap a font description#152025
serhiy-storchaka merged 4 commits into
python:mainfrom
serhiy-storchaka:tkinter-font-wrap-description

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Jun 23, 2026

Copy link
Copy Markdown
Member

By default :class:tkinter.font.Font creates a new named font from the given description (via Tcl's font create) and then uses its name in :meth:~tkinter.font.Font.actual, :meth:~tkinter.font.Font.measure and :meth:~tkinter.font.Font.metrics. There is a loss of precision between the description and the created font, so a Font object can behave differently from the equivalent description tuple (gh-143990).

This adds a wrap mode: with exists=True and no name, the font description is wrapped as is, without creating a named font, so that it is used without loss of precision and renders identically to the raw description when used as a widget option. In this case the :attr:!name attribute is the description itself rather than a string (formatted via tkinter._join when used as an option value, the same way ttk formats its style values).

Keeping the description as name (rather than a stringified form) keeps __eq__ correct: a wrapped description never accidentally equals a named font whose name happens to be its string form.

Keyword options now also override the corresponding attributes of the given font instead of being silently ignored.

(There is also an open PR #143992 taking a narrower "preserve negative pixel sizes" approach to the same issue; this is the more general fix.)

🤖 Generated with Claude Code

With exists=True and no name, Font now wraps the font description as is,
without creating a new named font, so that it is used without loss of
precision by actual(), measure() and metrics().  Its name attribute is then
the description rather than a string.  Keyword options now override the
corresponding settings of the given font instead of being ignored.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@read-the-docs-community

read-the-docs-community Bot commented Jun 23, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33474015 | 📁 Comparing 0edf788 against main (7928a8b)

  🔍 Preview build  

66 files changed · ± 65 modified · - 1 deleted

± Modified

- Deleted

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
serhiy-storchaka and others added 2 commits July 7, 2026 10:58
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…escription

# Conflicts:
#	Doc/whatsnew/3.16.rst
@serhiy-storchaka serhiy-storchaka enabled auto-merge (squash) July 7, 2026 08:01
@serhiy-storchaka

Copy link
Copy Markdown
Member Author

I merge it because I need it for other feature, #72880.

@serhiy-storchaka serhiy-storchaka merged commit 2cd5b79 into python:main Jul 7, 2026
93 of 95 checks passed
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Jul 8, 2026
Drop the assertion in test_create_from_named_font that explicit options
override the copied font settings: that behavior was added by pythonGH-152025
and is not in 3.15, where options are ignored when a font is specified.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant