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

Conversation

Xiaokang2022
Copy link
Contributor

@Xiaokang2022 Xiaokang2022 commented Jan 8, 2025

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Needs a test.

Comment on lines 2744 to 2745
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.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

$ has a special meaning in Tcl. It is better to avoid it. Use anything other, e.g. ! (- is already used in distinct scheme for Checkbutton).

Alternatively, we can change the scheme more radically -- using '%s!%d' instead of '!%s%d'. But this needs more thoughts.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

The test does not cover all code. Well, the existing test also does not cover all old code.

Add self.assertNotEqual(str(f), str(f2)) -- this will test the uniqueness of names for old scheme.

Now you need to create several instances of Button and Button2 (or Frame and Frame2, as there are already two Frame instances) and check that their names are unique, i.e. check the length of the set of their str() representations.

For now, the test is passed with unpatched code, except the last added line which tests an implementation detail.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM.

@serhiy-storchaka serhiy-storchaka enabled auto-merge (squash) January 13, 2025 15:34
@serhiy-storchaka serhiy-storchaka merged commit da8825e into python:main Jan 13, 2025
38 checks passed
@miss-islington-app
Copy link

Thanks @Xiaokang2022 for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 13, 2025
…-128604)

There were possible conflicts if the widget class name ends with a digit.
(cherry picked from commit da8825e)

Co-authored-by: Zhikang Yan <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Jan 13, 2025

GH-128791 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jan 13, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 13, 2025
…-128604)

There were possible conflicts if the widget class name ends with a digit.
(cherry picked from commit da8825e)

Co-authored-by: Zhikang Yan <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Jan 13, 2025

GH-128792 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Jan 13, 2025
@Xiaokang2022 Xiaokang2022 deleted the fix-tkinter-widget-name branch January 13, 2025 15:57
serhiy-storchaka pushed a commit that referenced this pull request Jan 13, 2025
…) (GH-128791)

There were possible conflicts if the widget class name ends with a digit.
(cherry picked from commit da8825e)

Co-authored-by: Zhikang Yan <[email protected]>
serhiy-storchaka pushed a commit that referenced this pull request Jan 13, 2025
…) (GH-128792)

There were possible conflicts if the widget class name ends with a digit.
(cherry picked from commit da8825e)

Co-authored-by: Zhikang Yan <[email protected]>
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