aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorZhikang Yan <2951256653@qq.com>2025-05-02 19:38:50 +0800
committerGitHub <noreply@github.com>2025-05-02 14:38:50 +0300
commite490c00dac16d458bb66ad157fda2edd0a2d0a14 (patch)
treea98e0adb19341bd0abf7ebd41ce492f8ac207fdf
parent1e9cc3d502773d2dd3b420ecf5b949d902e010c3 (diff)
downloadcpython-e490c00dac16d458bb66ad157fda2edd0a2d0a14.tar.gz
cpython-e490c00dac16d458bb66ad157fda2edd0a2d0a14.zip
gh-130482: Add ability to specify name for tkinter.OptionMenu and tkinter.ttk.OptionMenu (GH-130502)
-rw-r--r--Doc/whatsnew/3.14.rst3
-rw-r--r--Lib/test/test_tkinter/test_widgets.py5
-rw-r--r--Lib/test/test_ttk/test_extensions.py6
-rw-r--r--Lib/tkinter/__init__.py2
-rw-r--r--Lib/tkinter/ttk.py3
-rw-r--r--Misc/NEWS.d/next/Library/2025-02-24-12-22-51.gh-issue-130482.p2DrrL.rst2
6 files changed, 19 insertions, 2 deletions
diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst
index c0a824ee8dd..0d2e6533d05 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 f6e77973061..ff3f92e9b5e 100644
--- a/Lib/test/test_tkinter/test_widgets.py
+++ b/Lib/test/test_tkinter/test_widgets.py
@@ -354,6 +354,11 @@ class OptionMenuTest(MenubuttonTest, unittest.TestCase):
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 d5e06971697..05bca59e703 100644
--- a/Lib/test/test_ttk/test_extensions.py
+++ b/Lib/test/test_ttk/test_extensions.py
@@ -319,6 +319,12 @@ class OptionMenuTest(AbstractTkTest, unittest.TestCase):
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 71a265e3532..a693b04870b 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -4199,7 +4199,7 @@ class OptionMenu(Menubutton):
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 8ddb7f97e3b..c0cf1e787fa 100644
--- a/Lib/tkinter/ttk.py
+++ b/Lib/tkinter/ttk.py
@@ -1603,7 +1603,8 @@ class OptionMenu(Menubutton):
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 00000000000..38a1e81770f
--- /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`.