Skip to content

gh-156961: Fix tkinter.font.Font for a font name returned as a Tcl object - #157028

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:tkinter-font-tcl-object
Open

gh-156961: Fix tkinter.font.Font for a font name returned as a Tcl object#157028
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:tkinter-font-tcl-object

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Sep 6, 2026

Copy link
Copy Markdown
Member

Tk can return a font name or description as a Tcl object rather than a string, for example from ttk.Style().lookup("TButton", "font"), Menu.entrycget("font"), ttk.Entry.cget("font"), or the default value in the result of configure(). Such an object does not compare equal to a string, so it was not recognized as the name of an existing named font, and the code from the issue failed with TclError: named font TkDefaultFont does not already exist. Wrapping a font description also failed, with TypeError: '_tkinter.Tcl_Obj' object is not iterable.

The object is now kept as is, so that it is passed back to Tk, which reuses the font resource cached in it (this is why FromObj() does not convert fonts to strings, see gh-153513). It is only converted where it is compared with a string.

Tested with Tcl/Tk 8.6.17, 8.7b1, 9.0.4 and 9.1b1, with and without wantobjects.

The backports to 3.15, 3.14 and 3.13 need to be made manually: these branches have no wrapped font descriptions (gh-143990), so Font.name is always a string there and __str__ needs a different fix.

🤖 Generated with Claude Code

…Tcl object

Tk can return a font name or description as a Tcl object, for example
from ttk.Style().lookup("TButton", "font"), Menu.entrycget("font"),
ttk.Entry.cget("font"), or the default value in the result of
configure().  Such an object does not compare equal to a string, so it
was not recognized as the name of an existing named font.  Keep it as
is, so that it is passed back to Tk, and only convert it where it is
compared with a string.
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 6, 2026
@serhiy-storchaka serhiy-storchaka added needs backport to 3.14 bugs and security fixes needs backport to 3.13 bugs and security fixes labels Sep 6, 2026
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.13 bugs and security fixes needs backport to 3.14 bugs and security fixes 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