Skip to content

Commit 04e6146

Browse files
[3.12] gh-116484: Fix collisions between Checkbutton and ttk.Checkbutton default names (GH-116495) (GH-116901)
Change automatically generated tkinter.Checkbutton widget names to avoid collisions with automatically generated tkinter.ttk.Checkbutton widget names within the same parent widget. (cherry picked from commit c61cb50) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 716d482 commit 04e6146

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Lib/test/test_ttk/test_widgets.py

Lines changed: 21 additions & 1 deletion
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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3074,11 +3074,16 @@ def __init__(self, master=None, cnf={}, **kw):
30743074
Widget.__init__(self, master, 'checkbutton', cnf, kw)
30753075

30763076
def _setup(self, master, cnf):
3077+
# Because Checkbutton defaults to a variable with the same name as
3078+
# the widget, Checkbutton default names must be globally unique,
3079+
# not just unique within the parent widget.
30773080
if not cnf.get('name'):
30783081
global _checkbutton_count
30793082
name = self.__class__.__name__.lower()
30803083
_checkbutton_count += 1
3081-
cnf['name'] = f'!{name}{_checkbutton_count}'
3084+
# To avoid collisions with ttk.Checkbutton, use the different
3085+
# name template.
3086+
cnf['name'] = f'!{name}-{_checkbutton_count}'
30823087
super()._setup(master, cnf)
30833088

30843089
def deselect(self):
Lines changed: 3 additions & 0 deletions
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)