Skip to content

tkinter missing TkDefaultFont #156961

Closed
Closed

Description

@jvalenzuela

Bug report

Bug description:

Encountering problems looking up TkDefaultFont with Python 3.14.7 on Windows 10(x64), possibly due to the upgrade to Tcl/Tk 9.0.4 that occurred in the 3.14.7 Windows release ref.

The example script below demonstrates the problem:

import tkinter as tk
from tkinter import ttk
import tkinter.font as tkfont

app = tk.Tk()
style = ttk.Style()
font_name = style.lookup("TButton", "font")
font = tkfont.nametofont(font_name)
print(f"{font_name} is {font}")
app.mainloop()

This fails with the following traceback:

Traceback (most recent call last):
  File "C:\Users\jason\Documents\atform\master\font.py", line 8, in <module>
    font = tkfont.nametofont(font_name)
  File "C:\Users\jason\AppData\Local\Programs\Python\Python314\Lib\tkinter\font.py", line 25, in nametofont
    return Font(name=name, exists=True, root=root)
  File "C:\Users\jason\AppData\Local\Programs\Python\Python314\Lib\tkinter\font.py", line 96, in __init__
    raise tkinter._tkinter.TclError(
        "named font %s does not already exist" % (self.name,))
_tkinter.TclError: named font TkDefaultFont does not already exist

This example runs normally on Python versions up to and including 3.14.6, but fails on 3.14.7.

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Linked PRs

Activity

  1. terryjreedy commented on Sep 5, 2026

    @terryjreedy
    Member

    Since the failure is in nametofont, the last 2 lines are not needed. Verified in 3.14.7, 3.15.0rc2, and 3.16.0-.

    IDLE still successfully finds TkDefaultFont in idlelib.help line 177:
    normalfont = self.findfont(['TkDefaultFont', 'arial', 'helvetica'])
    self is an instance of a Text subclass with this method:

        def findfont(self, names):
            "Return name of first font family derived from names."
            for name in names:
                if name.lower() in (x.lower() for x in tkfont.names(root=self)):
                    font = tkfont.Font(name=name, exists=True, root=self)
                    return font.actual()['family']
                elif name.lower() in (x.lower()
                                      for x in tkfont.families(root=self)):
                    return name
    

    I am pretty sure I did not write this.

  2. zware commented on Sep 5, 2026

    @zware
    Member

    It turns out that this does seem to be a Tcl/Tk 9 change; style.lookup("TButton", "font") no longer returns a str, but a Tcl_Obj wrapping a Tk font object. The good news is that there's a simple workaround; call nametofont(str(font_name)).

    It might be worth fixing nametofont to accept a Tcl_Obj whose typename is font (or otherwise just call str on name), but I think we need @serhiy-storchaka's opinion there.

  3. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Sep 5, 2026
  4. jvalenzuela commented on Sep 5, 2026

    @jvalenzuela
    Author

    Perhaps it may be better to apply the conversion to the return value of style.lookup() so other uses of that function continue to receive a string. Additionally, the name of the function nametofont() does somewhat imply conversion of a name(str) to a font, not a font to a font.

  5. zware commented on Sep 5, 2026

    @zware
    Member

    See also gh-153513, where Serhiy specifically left font objects as Tcl_Obj in the changes for that issue.

  6. added a commit that references this issue on Sep 13, 2026
  7. added 2 commits that reference this issue on Sep 13, 2026
  8. added a commit that references this issue on Sep 14, 2026
  9. added a commit that references this issue on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-tkintertype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions