Skip to content

Commit c61cb50

Browse files
gh-116484: Fix collisions between Checkbutton and ttk.Checkbutton default names (GH-116495)
Change automatically generated tkinter.Checkbutton widget names to avoid collisions with automatically generated tkinter.ttk.Checkbutton widget names within the same parent widget.
1 parent 1069a46 commit c61cb50

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Lib/test/test_ttk/test_widgets.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,29 @@ def test_unique_variables(self):
285285
b.pack()
286286
buttons.append(b)
287287
variables = [str(b['variable']) for b in buttons]
288-
print(variables)
289288
self.assertEqual(len(set(variables)), 4, variables)
290289

290+
def test_unique_variables2(self):
291+
buttons = []
292+
f = ttk.Frame(self.root)
293+
f.pack()
294+
f = ttk.Frame(self.root)
295+
f.pack()
296+
for j in 'AB':
297+
b = tkinter.Checkbutton(f, text=j)
298+
b.pack()
299+
buttons.append(b)
300+
# Should be larger than the number of all previously created
301+
# tkinter.Checkbutton widgets:
302+
for j in range(100):
303+
b = ttk.Checkbutton(f, text=str(j))
304+
b.pack()
305+
buttons.append(b)
306+
names = [str(b) for b in buttons]
307+
self.assertEqual(len(set(names)), len(buttons), names)
308+
variables = [str(b['variable']) for b in buttons]
309+
self.assertEqual(len(set(variables)), len(buttons), variables)
310+
291311

292312
@add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
293313
class EntryTest(AbstractWidgetTest, unittest.TestCase):

Lib/tkinter/__init__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -3166,11 +3166,16 @@ def __init__(self, master=None, cnf={}, **kw):
31663166
Widget.__init__(self, master, 'checkbutton', cnf, kw)
31673167

31683168
def _setup(self, master, cnf):
3169+
# Because Checkbutton defaults to a variable with the same name as
3170+
# the widget, Checkbutton default names must be globally unique,
3171+
# not just unique within the parent widget.
31693172
if not cnf.get('name'):
31703173
global _checkbutton_count
31713174
name = self.__class__.__name__.lower()
31723175
_checkbutton_count += 1
3173-
cnf['name'] = f'!{name}{_checkbutton_count}'
3176+
# To avoid collisions with ttk.Checkbutton, use the different
3177+
# name template.
3178+
cnf['name'] = f'!{name}-{_checkbutton_count}'
31743179
super()._setup(master, cnf)
31753180

31763181
def deselect(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Change automatically generated :class:`tkinter.Checkbutton` widget names to
2+
avoid collisions with automatically generated
3+
:class:`tkinter.ttk.Checkbutton` widget names within the same parent widget.

0 commit comments

Comments
 (0)