summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-10-21 13:59:20 -0700
committerGitHub <noreply@github.com>2021-10-21 22:59:20 +0200
commit04485ac9886cd807aae854fb905a88c791cfdde6 (patch)
treea542c07d6bb9c455b46693d6449deec5d05541f6
parente628700dbf2c3376502cbb5a9bff2d58d1102e16 (diff)
downloadcpython-git-04485ac9886cd807aae854fb905a88c791cfdde6.tar.gz
bpo-45160: Ttk optionmenu only set variable once (GH-28291) (GH-29132)
(cherry picked from commit add46f84769a7e6fafa50954f79b7c248231fa4e) Co-authored-by: E-Paine <63801254+E-Paine@users.noreply.github.com>
-rw-r--r--Lib/tkinter/test/test_ttk/test_extensions.py13
-rw-r--r--Lib/tkinter/ttk.py5
-rw-r--r--Misc/NEWS.d/next/Library/2021-09-11-14-47-05.bpo-45160.VzMXbW.rst1
3 files changed, 18 insertions, 1 deletions
diff --git a/Lib/tkinter/test/test_ttk/test_extensions.py b/Lib/tkinter/test/test_ttk/test_extensions.py
index 438d21d0b3..cddd1f2e84 100644
--- a/Lib/tkinter/test/test_ttk/test_extensions.py
+++ b/Lib/tkinter/test/test_ttk/test_extensions.py
@@ -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):
diff --git a/Lib/tkinter/ttk.py b/Lib/tkinter/ttk.py
index ab7aeb15e8..9b58497251 100644
--- a/Lib/tkinter/ttk.py
+++ b/Lib/tkinter/ttk.py
@@ -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
index 0000000000..9d11ed0e55
--- /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