Skip to content

Tkinter misleading error in Tk.__getitem__ #140481

@TheLizzard

Description

@TheLizzard

Bug report

Bug description:

import tkinter as tk
root = tk.Tk()
print("background" in root)

Prints the following stack trace:

Traceback (most recent call last):
  File "/example.py", line 3, in <module>
    print("background" in root)
          ^^^^^^^^^^^^^^^^^^^^
  File "/tkinter/__init__.py", line 1727, in cget
    return self.tk.call(self._w, 'cget', '-' + key)
                                         ~~~~^~~~~
TypeError: can only concatenate str (not "int") to str

The TypeError referencing concatenation of an int to a str is very misleading. Error messages that don't make sense can be very confusing especially for beginners who don't know about magic methods like __iter__ and __getitem__.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-tkintertype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions