Skip to content

Commit e71d7ec

Browse files
committed
Fix ttk.OptionMenu command callback parameter type
ttk.OptionMenu invokes its command callback with the selected string value (set_menu passes val from *values), not a StringVar. Match the annotation to tkinter.OptionMenu, which already uses str.
1 parent 0bf68a8 commit e71d7ec

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

‎stdlib/@tests/test_cases/check_tkinter.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import sys
44
import tkinter
5+
import tkinter.ttk
56
import traceback
67
import types
78
from typing_extensions import assert_type
@@ -16,6 +17,15 @@ def custom_handler(exc: type[BaseException], val: BaseException, tb: types.Trace
1617
root.report_callback_exception = custom_handler
1718

1819

20+
# OptionMenu passes the selected string value to its command callback.
21+
def on_select(value: str) -> None:
22+
print(value)
23+
24+
25+
tkinter.OptionMenu(root, tkinter.StringVar(), "a", "b", command=on_select)
26+
tkinter.ttk.OptionMenu(root, tkinter.StringVar(), "a", "a", "b", command=on_select)
27+
28+
1929
def foo(x: int, y: str) -> None:
2030
pass
2131

‎stdlib/tkinter/ttk.pyi‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,7 @@ class OptionMenu(Menubutton):
14141414
# rest of these are keyword-only because *args syntax used above
14151415
style: str = "",
14161416
direction: Literal["above", "below", "left", "right", "flush"] = "below",
1417-
command: Callable[[tkinter.StringVar], object] | None = None,
1417+
command: Callable[[str], object] | None = None,
14181418
name: str | None = None,
14191419
) -> None: ...
14201420
else:
@@ -1427,7 +1427,7 @@ class OptionMenu(Menubutton):
14271427
# rest of these are keyword-only because *args syntax used above
14281428
style: str = "",
14291429
direction: Literal["above", "below", "left", "right", "flush"] = "below",
1430-
command: Callable[[tkinter.StringVar], object] | None = None,
1430+
command: Callable[[str], object] | None = None,
14311431
) -> None: ...
14321432
# configure, config, cget, destroy are inherited from Menubutton
14331433
# destroy and __setitem__ are overridden, signature does not change

0 commit comments

Comments
 (0)