]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-116484: Fix collisions between Checkbutton and ttk.Checkbutton default names ...
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 16 Mar 2024 11:31:19 +0000 (13:31 +0200)
committerGitHub <noreply@github.com>
Sat, 16 Mar 2024 11:31:19 +0000 (13:31 +0200)
Change automatically generated tkinter.Checkbutton widget names to
avoid collisions with automatically generated tkinter.ttk.Checkbutton
widget names within the same parent widget.

Lib/test/test_ttk/test_widgets.py
Lib/tkinter/__init__.py
Misc/NEWS.d/next/Library/2024-03-08-11-31-49.gh-issue-116484.VMAsU7.rst [new file with mode: 0644]

index fd1a748a498ac51380871e6a6cab49ed33f987f1..e3e440c45859f7a338e59a8e5d4a9df6d49a93f5 100644 (file)
@@ -285,9 +285,29 @@ class CheckbuttonTest(AbstractLabelTest, unittest.TestCase):
                 b.pack()
                 buttons.append(b)
         variables = [str(b['variable']) for b in buttons]
-        print(variables)
         self.assertEqual(len(set(variables)), 4, variables)
 
+    def test_unique_variables2(self):
+        buttons = []
+        f = ttk.Frame(self.root)
+        f.pack()
+        f = ttk.Frame(self.root)
+        f.pack()
+        for j in 'AB':
+            b = tkinter.Checkbutton(f, text=j)
+            b.pack()
+            buttons.append(b)
+        # Should be larger than the number of all previously created
+        # tkinter.Checkbutton widgets:
+        for j in range(100):
+            b = ttk.Checkbutton(f, text=str(j))
+            b.pack()
+            buttons.append(b)
+        names = [str(b) for b in buttons]
+        self.assertEqual(len(set(names)), len(buttons), names)
+        variables = [str(b['variable']) for b in buttons]
+        self.assertEqual(len(set(variables)), len(buttons), variables)
+
 
 @add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
 class EntryTest(AbstractWidgetTest, unittest.TestCase):
index 175bfbd7d912d2e99d2b4b3b0f7a3f927b043b3f..fd7b48e351999001088688355eae24c310b2d649 100644 (file)
@@ -3166,11 +3166,16 @@ class Checkbutton(Widget):
         Widget.__init__(self, master, 'checkbutton', cnf, kw)
 
     def _setup(self, master, cnf):
+        # Because Checkbutton defaults to a variable with the same name as
+        # the widget, Checkbutton default names must be globally unique,
+        # not just unique within the parent widget.
         if not cnf.get('name'):
             global _checkbutton_count
             name = self.__class__.__name__.lower()
             _checkbutton_count += 1
-            cnf['name'] = f'!{name}{_checkbutton_count}'
+            # To avoid collisions with ttk.Checkbutton, use the different
+            # name template.
+            cnf['name'] = f'!{name}-{_checkbutton_count}'
         super()._setup(master, cnf)
 
     def deselect(self):
diff --git a/Misc/NEWS.d/next/Library/2024-03-08-11-31-49.gh-issue-116484.VMAsU7.rst b/Misc/NEWS.d/next/Library/2024-03-08-11-31-49.gh-issue-116484.VMAsU7.rst
new file mode 100644 (file)
index 0000000..265c381
--- /dev/null
@@ -0,0 +1,3 @@
+Change automatically generated :class:`tkinter.Checkbutton` widget names to
+avoid collisions with automatically generated
+:class:`tkinter.ttk.Checkbutton` widget names within the same parent widget.