Skip to content

gh-128562: Fix tkinter widget instance name sometimes duplicated on inherited class #128604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 13, 2025
5 changes: 4 additions & 1 deletion Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2740,7 +2740,10 @@ def _setup(self, master, cnf):
name = cnf['name']
del cnf['name']
if not name:
name = self.__class__.__name__.lower()
cls = self.__class__
while cls.__module__ not in ("tkinter", "tkinter.ttk"):
cls = cls.__base__
Copy link
Member

Choose a reason for hiding this comment

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

This works, but it's a little hacky, and probably going to lead to some unexpected results. The more ideal solution would be to actually check if the autogenerated name exists and then adjust accordingly (e.g. label2 could become label2_2).

Copy link
Contributor Author

@Xiaokang2022 Xiaokang2022 Jan 10, 2025

Choose a reason for hiding this comment

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

The more ideal solution would be to actually check if the autogenerated name exists and then adjust accordingly

However, this will cause the _name of the original normal widgets to be changed, which may lead to other incompatibilities. I'd prefer to just modify the name of the inherited class.

I now think that if the class name ends with a number, then we might be best able to put a special tag (This tag cannot appear in the class name) after its name. This may be a better solution. (e.g. label2 -> label2$)

What do you think of this solution?

Copy link
Contributor Author

@Xiaokang2022 Xiaokang2022 Jan 10, 2025

Choose a reason for hiding this comment

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

I now think that if the class name ends with a number, then we might be best able to put a special tag (This tag cannot appear in the class name) after its name.

The existing test code requires that the name must be a valid identifier. So there may not be a way to do that.

I'm not quite sure what to do right now is the best thing to do. :(

Sorry, I read it wrong, the existing test code requires that name is not a valid identifier.

name = cls.__name__.lower()
if master._last_child_ids is None:
master._last_child_ids = {}
count = master._last_child_ids.get(name, 0) + 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the bug that the widgets of :mod:`tkinter` are named inaccurately.
Loading