Skip to content

Conversation

@TheLizzard
Copy link

@TheLizzard TheLizzard commented Oct 22, 2025

Before

Code like:

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

Would throw a TypeError: can only concatenate str (not "int") to str

Summary of changes

  • Fixed the error message when calling "option" in <tk.Misc>
  • Added __contain__ method to tk.Misc since it already has __getitem__ and __setitem__
  • Added tests to prevent regression

Testing

  • Ran ./python -m test test_tkinter -u=gui locally to make sure the old tests as well as the new ones pass

@picnixz picnixz changed the title gh-140481 gh-140481: avoid misleading exception in tk.Tk.__getitem__ Oct 22, 2025
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is the correct way to fix this. We should instead check if option is a string in cget and if not raise a TypeError here.

cc @serhiy-storchaka

@TheLizzard
Copy link
Author

TheLizzard commented Oct 22, 2025

That might be better but then for consistency sake, we would have to add a lot of type checks to functions like Misc.config since right now they raise TclError and not TypeError (root.config(background=frozenset()).

I don't know what the best course of action is.

@serhiy-storchaka
Copy link
Member

See #140501.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants