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
10 changes: 9 additions & 1 deletion Lib/test/test_tkinter/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ def test_repr(self):
self.assertEqual(repr(f), '<tkinter.Frame object .top.child>')

def test_generated_names(self):
class Button2(tkinter.Button):
pass

t = tkinter.Toplevel(self.root)
f = tkinter.Frame(t)
f2 = tkinter.Frame(t)
self.assertNotEqual(str(f), str(f2))
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))
b3 = tkinter.Button(f2)
b4 = Button2(f2)
self.assertEqual(len({str(b), str(b2), str(b3), str(b4)}), 4)

@requires_tk(8, 6, 6)
def test_tk_busy(self):
Expand Down
2 changes: 2 additions & 0 deletions Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2741,6 +2741,8 @@ def _setup(self, master, cnf):
del cnf['name']
if not name:
name = self.__class__.__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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix possible conflicts in generated :mod:`tkinter` widget names if the widget class name ends with a digit.
Loading