From 37a36a1b64cea1f5ddd01479fc8dd2e726fe7256 Mon Sep 17 00:00:00 2001 From: Xiaokang2022 <2951256653@qq.com> Date: Wed, 8 Jan 2025 10:47:28 +0800 Subject: [PATCH 01/11] fix: Fixed a bug with auto-naming --- Lib/tkinter/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py index d494c0c9687cd1..d543a1aeb667ea 100644 --- a/Lib/tkinter/__init__.py +++ b/Lib/tkinter/__init__.py @@ -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__ + name = cls.__name__.lower() if master._last_child_ids is None: master._last_child_ids = {} count = master._last_child_ids.get(name, 0) + 1 From b46da92fa294449302858d28a8923105fdfc08cf Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 8 Jan 2025 03:09:31 +0000 Subject: [PATCH 02/11] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst diff --git a/Misc/NEWS.d/next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst b/Misc/NEWS.d/next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst new file mode 100644 index 00000000000000..8cae3f359105ea --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst @@ -0,0 +1 @@ +Fix the bug that the widgets of :mod:`tkinter` are named inaccurately. From 2478aa42dc3f4aed1a6c794850630efbf74fb9c7 Mon Sep 17 00:00:00 2001 From: Xiaokang2022 <2951256653@qq.com> Date: Fri, 10 Jan 2025 16:47:05 +0800 Subject: [PATCH 03/11] refactor: Refactor the solution --- Lib/tkinter/__init__.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py index d543a1aeb667ea..86a1b27dc7c799 100644 --- a/Lib/tkinter/__init__.py +++ b/Lib/tkinter/__init__.py @@ -2740,10 +2740,9 @@ def _setup(self, master, cnf): name = cnf['name'] del cnf['name'] if not name: - cls = self.__class__ - while cls.__module__ not in ("tkinter", "tkinter.ttk"): - cls = cls.__base__ name = cls.__name__.lower() + if name[-1].isdigit(): + name += "$" # Avoid duplication when calculating names below if master._last_child_ids is None: master._last_child_ids = {} count = master._last_child_ids.get(name, 0) + 1 From d0c90b7d879034365564440896cc83aa30785312 Mon Sep 17 00:00:00 2001 From: Xiaokang2022 <2951256653@qq.com> Date: Fri, 10 Jan 2025 17:10:34 +0800 Subject: [PATCH 04/11] fix: Use the better solution --- Lib/tkinter/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py index 86a1b27dc7c799..11f66e84f14dae 100644 --- a/Lib/tkinter/__init__.py +++ b/Lib/tkinter/__init__.py @@ -2742,7 +2742,7 @@ def _setup(self, master, cnf): if not name: name = cls.__name__.lower() if name[-1].isdigit(): - name += "$" # Avoid duplication when calculating names below + name += "_0" # Avoid duplication when calculating names below if master._last_child_ids is None: master._last_child_ids = {} count = master._last_child_ids.get(name, 0) + 1 From 2e68dec6456bac60f14b01a9747908635d2fdc38 Mon Sep 17 00:00:00 2001 From: Xiaokang2022 <2951256653@qq.com> Date: Fri, 10 Jan 2025 17:16:34 +0800 Subject: [PATCH 05/11] test: Add tests --- Lib/test/test_tkinter/test_misc.py | 7 ++++++- Lib/tkinter/__init__.py | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_tkinter/test_misc.py b/Lib/test/test_tkinter/test_misc.py index 3362169391818b..600299a8aeaef1 100644 --- a/Lib/test/test_tkinter/test_misc.py +++ b/Lib/test/test_tkinter/test_misc.py @@ -31,12 +31,17 @@ def test_repr(self): self.assertEqual(repr(f), '') def test_generated_names(self): + class Button2(tkinter.Button): + pass + t = tkinter.Toplevel(self.root) f = tkinter.Frame(t) f2 = tkinter.Frame(t) b = tkinter.Button(f2) - for name in str(b).split('.'): + b2 = Button2(f2) + for name in str(b).split('.') + str(b2).split('.'): self.assertFalse(name.isidentifier(), msg=repr(name)) + self.assertEqual(str(b2)[-1], "$") @requires_tk(8, 6, 6) def test_tk_busy(self): diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py index 11f66e84f14dae..86a1b27dc7c799 100644 --- a/Lib/tkinter/__init__.py +++ b/Lib/tkinter/__init__.py @@ -2742,7 +2742,7 @@ def _setup(self, master, cnf): if not name: name = cls.__name__.lower() if name[-1].isdigit(): - name += "_0" # Avoid duplication when calculating names below + name += "$" # Avoid duplication when calculating names below if master._last_child_ids is None: master._last_child_ids = {} count = master._last_child_ids.get(name, 0) + 1 From 8353a114afe8f8a3cf7eedf00c0bd90b6670773b Mon Sep 17 00:00:00 2001 From: Xiaokang2022 <2951256653@qq.com> Date: Fri, 10 Jan 2025 17:19:15 +0800 Subject: [PATCH 06/11] fix --- Lib/tkinter/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py index 86a1b27dc7c799..c31ea38a60f847 100644 --- a/Lib/tkinter/__init__.py +++ b/Lib/tkinter/__init__.py @@ -2740,7 +2740,7 @@ def _setup(self, master, cnf): name = cnf['name'] del cnf['name'] if not name: - name = cls.__name__.lower() + name = self.__class__.__name__.lower() if name[-1].isdigit(): name += "$" # Avoid duplication when calculating names below if master._last_child_ids is None: From 4e9d5f147ea26d28b00b62c63f7ed7e54e41b205 Mon Sep 17 00:00:00 2001 From: Xiaokang2022 <2951256653@qq.com> Date: Mon, 13 Jan 2025 20:37:01 +0800 Subject: [PATCH 07/11] Change "$" to "!" --- Lib/test/test_tkinter/test_misc.py | 2 +- Lib/tkinter/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_tkinter/test_misc.py b/Lib/test/test_tkinter/test_misc.py index 600299a8aeaef1..458d6806f11142 100644 --- a/Lib/test/test_tkinter/test_misc.py +++ b/Lib/test/test_tkinter/test_misc.py @@ -41,7 +41,7 @@ class Button2(tkinter.Button): b2 = Button2(f2) for name in str(b).split('.') + str(b2).split('.'): self.assertFalse(name.isidentifier(), msg=repr(name)) - self.assertEqual(str(b2)[-1], "$") + self.assertEqual(str(b2)[-1], "!") @requires_tk(8, 6, 6) def test_tk_busy(self): diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py index c31ea38a60f847..0baed8b569e40f 100644 --- a/Lib/tkinter/__init__.py +++ b/Lib/tkinter/__init__.py @@ -2742,7 +2742,7 @@ def _setup(self, master, cnf): if not name: name = self.__class__.__name__.lower() if name[-1].isdigit(): - name += "$" # Avoid duplication when calculating names below + name += "!" # Avoid duplication when calculating names below if master._last_child_ids is None: master._last_child_ids = {} count = master._last_child_ids.get(name, 0) + 1 From 30e1ee37c044946e8403b2dd4479ede52b809647 Mon Sep 17 00:00:00 2001 From: Xiaokang2022 <2951256653@qq.com> Date: Mon, 13 Jan 2025 21:06:39 +0800 Subject: [PATCH 08/11] Improve some tests --- Lib/test/test_tkinter/test_misc.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_tkinter/test_misc.py b/Lib/test/test_tkinter/test_misc.py index 458d6806f11142..7dc808a5ac594d 100644 --- a/Lib/test/test_tkinter/test_misc.py +++ b/Lib/test/test_tkinter/test_misc.py @@ -37,11 +37,15 @@ class Button2(tkinter.Button): t = tkinter.Toplevel(self.root) f = tkinter.Frame(t) f2 = tkinter.Frame(t) + self.assertNotEqual(str(f), str(f2)) b = tkinter.Button(f2) b2 = Button2(f2) for name in str(b).split('.') + str(b2).split('.'): self.assertFalse(name.isidentifier(), msg=repr(name)) self.assertEqual(str(b2)[-1], "!") + b3 = tkinter.Button(f2) + b4 = Button2(f2) + self.assertEqual(len(set([str(b), str(b2), str(b3), str(b4)])), 4) @requires_tk(8, 6, 6) def test_tk_busy(self): From 078c5fca933b076d6d206fbf6faa537d56543dc6 Mon Sep 17 00:00:00 2001 From: Zhikang Yan <2951256653@qq.com> Date: Mon, 13 Jan 2025 21:15:42 +0800 Subject: [PATCH 09/11] Update Lib/test/test_tkinter/test_misc.py Co-authored-by: Serhiy Storchaka --- Lib/test/test_tkinter/test_misc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_tkinter/test_misc.py b/Lib/test/test_tkinter/test_misc.py index 7dc808a5ac594d..7ef579dec3186a 100644 --- a/Lib/test/test_tkinter/test_misc.py +++ b/Lib/test/test_tkinter/test_misc.py @@ -45,7 +45,7 @@ class Button2(tkinter.Button): self.assertEqual(str(b2)[-1], "!") b3 = tkinter.Button(f2) b4 = Button2(f2) - self.assertEqual(len(set([str(b), str(b2), str(b3), str(b4)])), 4) + self.assertEqual(len({str(b), str(b2), str(b3), str(b4)}), 4) @requires_tk(8, 6, 6) def test_tk_busy(self): From 60b162ae0df29fe1d4ce6db09ced40cd20d6b2bf Mon Sep 17 00:00:00 2001 From: Xiaokang2022 <2951256653@qq.com> Date: Mon, 13 Jan 2025 21:18:36 +0800 Subject: [PATCH 10/11] Improve the test --- Lib/test/test_tkinter/test_misc.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_tkinter/test_misc.py b/Lib/test/test_tkinter/test_misc.py index 7ef579dec3186a..96ea3f0117ca03 100644 --- a/Lib/test/test_tkinter/test_misc.py +++ b/Lib/test/test_tkinter/test_misc.py @@ -42,7 +42,6 @@ class Button2(tkinter.Button): b2 = Button2(f2) for name in str(b).split('.') + str(b2).split('.'): self.assertFalse(name.isidentifier(), msg=repr(name)) - self.assertEqual(str(b2)[-1], "!") b3 = tkinter.Button(f2) b4 = Button2(f2) self.assertEqual(len({str(b), str(b2), str(b3), str(b4)}), 4) From 70f1f2333bd6329387bfd0962121e23424a00fe7 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 13 Jan 2025 17:30:36 +0200 Subject: [PATCH 11/11] Update Misc/NEWS.d/next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst --- .../next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst b/Misc/NEWS.d/next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst index 8cae3f359105ea..eb50dded67bea8 100644 --- a/Misc/NEWS.d/next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst +++ b/Misc/NEWS.d/next/Library/2025-01-08-03-09-29.gh-issue-128562.Mlv-yO.rst @@ -1 +1 @@ -Fix the bug that the widgets of :mod:`tkinter` are named inaccurately. +Fix possible conflicts in generated :mod:`tkinter` widget names if the widget class name ends with a digit.