]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45160: Ttk optionmenu only set variable once (GH-28291) (GH-29132)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 21 Oct 2021 20:59:20 +0000 (13:59 -0700)
committerGitHub <noreply@github.com>
Thu, 21 Oct 2021 20:59:20 +0000 (22:59 +0200)
(cherry picked from commit add46f84769a7e6fafa50954f79b7c248231fa4e)

Co-authored-by: E-Paine <63801254+E-Paine@users.noreply.github.com>
Lib/tkinter/test/test_ttk/test_extensions.py
Lib/tkinter/ttk.py
Misc/NEWS.d/next/Library/2021-09-11-14-47-05.bpo-45160.VzMXbW.rst [new file with mode: 0644]

index 438d21d0b373340ba2f95a694c375a132e8ba2b5..cddd1f2e848347241cc4af3950238d3f854ee4a7 100644 (file)
@@ -301,6 +301,19 @@ class OptionMenuTest(AbstractTkTest, unittest.TestCase):
         optmenu.destroy()
         optmenu2.destroy()
 
+    def test_trace_variable(self):
+        # prior to bpo45160, tracing a variable would cause the callback to be made twice
+        success = []
+        items = ('a', 'b', 'c')
+        textvar = tkinter.StringVar(self.root)
+        def cb_test(*args):
+            self.assertEqual(textvar.get(), items[1])
+            success.append(True)
+        optmenu = ttk.OptionMenu(self.root, textvar, "a", *items)
+        textvar.trace("w", cb_test)
+        optmenu['menu'].invoke(1)
+        self.assertEqual(success, [True])
+
 
 class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase):
 
index ab7aeb15e8ff222e3b99bc56badba70ea0f17098..9b58497251d3854ed489517f3ca325b1b9311b6f 100644 (file)
@@ -1643,7 +1643,10 @@ class OptionMenu(Menubutton):
         menu.delete(0, 'end')
         for val in values:
             menu.add_radiobutton(label=val,
-                command=tkinter._setit(self._variable, val, self._callback),
+                command=(
+                    None if self._callback is None
+                    else lambda val=val: self._callback(val)
+                ),
                 variable=self._variable)
 
         if default:
diff --git a/Misc/NEWS.d/next/Library/2021-09-11-14-47-05.bpo-45160.VzMXbW.rst b/Misc/NEWS.d/next/Library/2021-09-11-14-47-05.bpo-45160.VzMXbW.rst
new file mode 100644 (file)
index 0000000..9d11ed0
--- /dev/null
@@ -0,0 +1 @@
+When tracing a tkinter variable used by a ttk OptionMenu, callbacks are no longer made twice.
\ No newline at end of file