https://github.com/python/cpython/commit/e490c00dac16d458bb66ad157fda2edd0a2d0a14
commit: e490c00dac16d458bb66ad157fda2edd0a2d0a14
branch: main
author: Zhikang Yan <2951256...@qq.com>
committer: serhiy-storchaka <storch...@gmail.com>
date: 2025-05-02T14:38:50+03:00
summary:

gh-130482: Add ability to specify name for tkinter.OptionMenu and 
tkinter.ttk.OptionMenu (GH-130502)

files:
A Misc/NEWS.d/next/Library/2025-02-24-12-22-51.gh-issue-130482.p2DrrL.rst
M Doc/whatsnew/3.14.rst
M Lib/test/test_tkinter/test_widgets.py
M Lib/test/test_ttk/test_extensions.py
M Lib/tkinter/__init__.py
M Lib/tkinter/ttk.py

diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst
index c0a824ee8ddf4d..0d2e6533d05f8d 100644
--- a/Doc/whatsnew/3.14.rst
+++ b/Doc/whatsnew/3.14.rst
@@ -1372,6 +1372,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
 ------
diff --git a/Lib/test/test_tkinter/test_widgets.py 
b/Lib/test/test_tkinter/test_widgets.py
index f6e77973061956..ff3f92e9b5ef83 100644
--- a/Lib/test/test_tkinter/test_widgets.py
+++ b/Lib/test/test_tkinter/test_widgets.py
@@ -354,6 +354,11 @@ 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.assertEqual(str(widget), ".option_menu")
+        self.assertIs(self.root.children["option_menu"], widget)
+
 @add_configure_tests(IntegerSizeTests, StandardOptionsTests)
 class EntryTest(AbstractWidgetTest, unittest.TestCase):
     _rounds_pixels = (tk_version < (9, 0))
diff --git a/Lib/test/test_ttk/test_extensions.py 
b/Lib/test/test_ttk/test_extensions.py
index d5e069716971fe..05bca59e703936 100644
--- a/Lib/test/test_ttk/test_extensions.py
+++ b/Lib/test/test_ttk/test_extensions.py
@@ -319,6 +319,12 @@ 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.assertEqual(str(widget), ".option_menu_ex")
+        self.assertIs(self.root.children["option_menu_ex"], widget)
+
 
 class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase):
 
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index 71a265e3532810..a693b04870b995 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -4199,7 +4199,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)
diff --git a/Lib/tkinter/ttk.py b/Lib/tkinter/ttk.py
index 8ddb7f97e3b233..c0cf1e787fa9ad 100644
--- a/Lib/tkinter/ttk.py
+++ b/Lib/tkinter/ttk.py
@@ -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)
 
diff --git 
a/Misc/NEWS.d/next/Library/2025-02-24-12-22-51.gh-issue-130482.p2DrrL.rst 
b/Misc/NEWS.d/next/Library/2025-02-24-12-22-51.gh-issue-130482.p2DrrL.rst
new file mode 100644
index 00000000000000..38a1e81770fbf3
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-02-24-12-22-51.gh-issue-130482.p2DrrL.rst
@@ -0,0 +1,2 @@
+Add ability to specify name for :class:`!tkinter.OptionMenu` and
+:class:`!tkinter.ttk.OptionMenu`.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to