Skip to content

Commit add46f8

Browse files
authored
bpo-45160: Ttk optionmenu only set variable once (GH-28291)
1 parent 0c4c2e6 commit add46f8

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Lib/tkinter/test/test_ttk/test_extensions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,19 @@ def test_unique_radiobuttons(self):
301301
optmenu.destroy()
302302
optmenu2.destroy()
303303

304+
def test_trace_variable(self):
305+
# prior to bpo45160, tracing a variable would cause the callback to be made twice
306+
success = []
307+
items = ('a', 'b', 'c')
308+
textvar = tkinter.StringVar(self.root)
309+
def cb_test(*args):
310+
self.assertEqual(textvar.get(), items[1])
311+
success.append(True)
312+
optmenu = ttk.OptionMenu(self.root, textvar, "a", *items)
313+
textvar.trace("w", cb_test)
314+
optmenu['menu'].invoke(1)
315+
self.assertEqual(success, [True])
316+
304317

305318
class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase):
306319

Lib/tkinter/ttk.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,10 @@ def set_menu(self, default=None, *values):
16431643
menu.delete(0, 'end')
16441644
for val in values:
16451645
menu.add_radiobutton(label=val,
1646-
command=tkinter._setit(self._variable, val, self._callback),
1646+
command=(
1647+
None if self._callback is None
1648+
else lambda val=val: self._callback(val)
1649+
),
16471650
variable=self._variable)
16481651

16491652
if default:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
When tracing a tkinter variable used by a ttk OptionMenu, callbacks are no longer made twice.

0 commit comments

Comments
 (0)