From b0f15d30a4b01bec2304de30858361554750917b Mon Sep 17 00:00:00 2001 From: E-Paine <63801254+E-Paine@users.noreply.github.com> Date: Sat, 11 Sep 2021 15:41:39 +0100 Subject: [PATCH 1/3] Ttk optionmenu not use _setit --- Lib/tkinter/ttk.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/tkinter/ttk.py b/Lib/tkinter/ttk.py index b854235a6267935..acdd565ec48a92c 100644 --- a/Lib/tkinter/ttk.py +++ b/Lib/tkinter/ttk.py @@ -1643,7 +1643,10 @@ def set_menu(self, default=None, *values): 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: From 826c4fd74ae4599bb6aa53f5b8e5be51f23cb33a Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 11 Sep 2021 14:47:06 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2021-09-11-14-47-05.bpo-45160.VzMXbW.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-09-11-14-47-05.bpo-45160.VzMXbW.rst 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 index 000000000000000..9d11ed0e55d24cf --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-09-11-14-47-05.bpo-45160.VzMXbW.rst @@ -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 From 13545094d4352bd2f7b8b7a2a181179c8ef41248 Mon Sep 17 00:00:00 2001 From: E-Paine <63801254+E-Paine@users.noreply.github.com> Date: Fri, 17 Sep 2021 17:31:03 +0100 Subject: [PATCH 3/3] Add test --- Lib/tkinter/test/test_ttk/test_extensions.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Lib/tkinter/test/test_ttk/test_extensions.py b/Lib/tkinter/test/test_ttk/test_extensions.py index e6b3eccf7afb8a0..4da3845e01301e6 100644 --- a/Lib/tkinter/test/test_ttk/test_extensions.py +++ b/Lib/tkinter/test/test_ttk/test_extensions.py @@ -301,6 +301,19 @@ def test_unique_radiobuttons(self): 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):