Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,9 @@ tkinter
arguments passed by keyword.
(Contributed by Zhikang Yan in :gh:`126899`.)

* Add ability to specify name for :class:`!tkinter.OptionMenu` and
:class:`!tkinter.ttk.OptionMenu`.
(Contributed by Zhikang Yan in :gh:`130482`.)

turtle
------
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_tkinter/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ def test_bad_kwarg(self):
with self.assertRaisesRegex(TclError, r"^unknown option -image$"):
tkinter.OptionMenu(self.root, None, 'b', image='')

def test_specify_name(self):
widget = tkinter.OptionMenu(self.root, None, ':)', name="option_menu")
self.assertIs(self.root.nametowidget("option_menu"), widget)

@add_configure_tests(IntegerSizeTests, StandardOptionsTests)
class EntryTest(AbstractWidgetTest, unittest.TestCase):
_rounds_pixels = (tk_version < (9, 0))
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_ttk/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ def cb_test(*args):
textvar.trace_remove("write", cb_name)
optmenu.destroy()

def test_specify_name(self):
textvar = tkinter.StringVar(self.root)
widget = ttk.OptionMenu(self.root, textvar, ":)", name="option_menu_ex")
self.assertIs(self.root.nametowidget("option_menu_ex"), widget)


class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase):

Expand Down
2 changes: 1 addition & 1 deletion Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4187,7 +4187,7 @@ def __init__(self, master, variable, value, *values, **kwargs):
keyword argument command."""
kw = {"borderwidth": 2, "textvariable": variable,
"indicatoron": 1, "relief": RAISED, "anchor": "c",
"highlightthickness": 2}
"highlightthickness": 2, "name": kwargs.pop("name", None)}
Widget.__init__(self, master, "menubutton", kw)
self.widgetName = 'tk_optionMenu'
menu = self.__menu = Menu(self, name="menu", tearoff=0)
Expand Down
3 changes: 2 additions & 1 deletion Lib/tkinter/ttk.py
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,8 @@ def __init__(self, master, variable, default=None, *values, **kwargs):
A callback that will be invoked after selecting an item.
"""
kw = {'textvariable': variable, 'style': kwargs.pop('style', None),
'direction': kwargs.pop('direction', None)}
'direction': kwargs.pop('direction', None),
'name': kwargs.pop('name', None)}
Menubutton.__init__(self, master, **kw)
self['menu'] = tkinter.Menu(self, tearoff=False)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add ability to specify name for :class:`!tkinter.OptionMenu` and
:class:`!tkinter.ttk.OptionMenu`.
Loading