From c9f70954d8214ca38ed33d1ea2979bad0a9f26ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=BA=B72022?= <2951256653@qq.com> Date: Tue, 29 Oct 2024 10:24:55 +0800 Subject: [PATCH 1/8] fix: `ttk.Widget.cget` has an incorrect return value type --- Lib/tkinter/ttk.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/tkinter/ttk.py b/Lib/tkinter/ttk.py index 073b3ae20797c3..226bf8a0adab60 100644 --- a/Lib/tkinter/ttk.py +++ b/Lib/tkinter/ttk.py @@ -26,7 +26,7 @@ "tclobjs_to_py", "setup_master"] import tkinter -from tkinter import _flatten, _join, _stringify, _splitdict +from tkinter import _flatten, _join, _splitdict, _stringify def _format_optvalue(value, script=False): @@ -570,6 +570,13 @@ def state(self, statespec=None): return self.tk.splitlist(str(self.tk.call(self._w, "state", statespec))) + def cget(self, key): + """Return the resource value for a KEY given as string.""" + return str(super().cget(key)) + + __getitem__ = cget + + class Button(Widget): """Ttk Button widget, displays a textual label and/or image, and evaluates a command when pressed.""" From f6281f862946464911bc6bb7577672fac9249788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=BA=B72022?= <2951256653@qq.com> Date: Tue, 29 Oct 2024 10:31:33 +0800 Subject: [PATCH 2/8] style --- Lib/tkinter/ttk.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/tkinter/ttk.py b/Lib/tkinter/ttk.py index 226bf8a0adab60..8e49461f5715ae 100644 --- a/Lib/tkinter/ttk.py +++ b/Lib/tkinter/ttk.py @@ -26,7 +26,7 @@ "tclobjs_to_py", "setup_master"] import tkinter -from tkinter import _flatten, _join, _splitdict, _stringify +from tkinter import _flatten, _join, _stringify, _splitdict def _format_optvalue(value, script=False): From 3cfd9ffa4b7604afc539cf9ff90717bdb97a4d12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=BA=B72022?= <2951256653@qq.com> Date: Tue, 29 Oct 2024 11:03:18 +0800 Subject: [PATCH 3/8] Fixed some bugs --- Lib/tkinter/ttk.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/tkinter/ttk.py b/Lib/tkinter/ttk.py index 8e49461f5715ae..0b685b6bd73508 100644 --- a/Lib/tkinter/ttk.py +++ b/Lib/tkinter/ttk.py @@ -26,6 +26,7 @@ "tclobjs_to_py", "setup_master"] import tkinter +import _tkinter from tkinter import _flatten, _join, _stringify, _splitdict @@ -572,7 +573,9 @@ def state(self, statespec=None): def cget(self, key): """Return the resource value for a KEY given as string.""" - return str(super().cget(key)) + if isinstance(return_value := super().cget(key), _tkinter.Tcl_Obj): + return str(return_value) + return return_value __getitem__ = cget From 194c0d584cf23a46f2276ed954a0e510f633a46a Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 29 Oct 2024 03:32:29 +0000 Subject: [PATCH 4/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-10-29-03-32-28.gh-issue-126008.wcuSmo.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-10-29-03-32-28.gh-issue-126008.wcuSmo.rst diff --git a/Misc/NEWS.d/next/Library/2024-10-29-03-32-28.gh-issue-126008.wcuSmo.rst b/Misc/NEWS.d/next/Library/2024-10-29-03-32-28.gh-issue-126008.wcuSmo.rst new file mode 100644 index 00000000000000..02bc8503e6552f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-29-03-32-28.gh-issue-126008.wcuSmo.rst @@ -0,0 +1 @@ +Fix the type of return value for `ttk.Widget.cget` From c040d1c2c6fc0e5fd98e07134cbf4d47b2725efb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=BA=B72022?= <2951256653@qq.com> Date: Tue, 29 Oct 2024 11:41:34 +0800 Subject: [PATCH 5/8] fix lint --- .../next/Library/2024-10-29-03-32-28.gh-issue-126008.wcuSmo.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-10-29-03-32-28.gh-issue-126008.wcuSmo.rst b/Misc/NEWS.d/next/Library/2024-10-29-03-32-28.gh-issue-126008.wcuSmo.rst index 02bc8503e6552f..7d37bded1249f2 100644 --- a/Misc/NEWS.d/next/Library/2024-10-29-03-32-28.gh-issue-126008.wcuSmo.rst +++ b/Misc/NEWS.d/next/Library/2024-10-29-03-32-28.gh-issue-126008.wcuSmo.rst @@ -1 +1 @@ -Fix the type of return value for `ttk.Widget.cget` +Fix an incorrect return value type when getting resource value of ttk widgets. From 9f0e79ac159b868f997b764f92943d5228965df7 Mon Sep 17 00:00:00 2001 From: Zhikang Yan <2951256653@qq.com> Date: Wed, 30 Oct 2024 03:35:47 +0800 Subject: [PATCH 6/8] style --- Lib/tkinter/ttk.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/tkinter/ttk.py b/Lib/tkinter/ttk.py index 0b685b6bd73508..71a61bcf4f8d15 100644 --- a/Lib/tkinter/ttk.py +++ b/Lib/tkinter/ttk.py @@ -573,7 +573,8 @@ def state(self, statespec=None): def cget(self, key): """Return the resource value for a KEY given as string.""" - if isinstance(return_value := super().cget(key), _tkinter.Tcl_Obj): + return_value = super().cget(key) + if isinstance(return_value, _tkinter.Tcl_Obj): return str(return_value) return return_value From 15cac9956638d38ce945b0c66a01365acd091a09 Mon Sep 17 00:00:00 2001 From: Xiaokang2022 <2951256653@qq.com> Date: Mon, 6 Jan 2025 12:32:58 +0800 Subject: [PATCH 7/8] test: Add a simple test for `ttk.Widget.cget` --- Lib/test/test_ttk/test_widgets.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/test/test_ttk/test_widgets.py b/Lib/test/test_ttk/test_widgets.py index d5620becfa7187..9135ab6b9cfe65 100644 --- a/Lib/test/test_ttk/test_widgets.py +++ b/Lib/test/test_ttk/test_widgets.py @@ -123,6 +123,12 @@ def test_cb(arg1, **kw): self.widget.state(['active', '!disabled']) self.assertEqual(self.widget.state(), ('active', )) + def test_widget_cget(self): + # Check whether the return value of cget is a string + self.assertIsInstance(self.widget.cget("width"), str) + self.assertEqual(self.widget.cget("state"), tkinter.NORMAL) + self.assertEqual(self.widget.cget("text"), "Text") + class AbstractToplevelTest(AbstractWidgetTest, PixelSizeTests): _rounds_pixels = False From aae1a3d8008eb1e6093362b8a9cff0111775ff1a Mon Sep 17 00:00:00 2001 From: Xiaokang2022 <2951256653@qq.com> Date: Mon, 6 Jan 2025 12:44:18 +0800 Subject: [PATCH 8/8] test: Fix tests --- Lib/test/test_ttk/test_widgets.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_ttk/test_widgets.py b/Lib/test/test_ttk/test_widgets.py index 9135ab6b9cfe65..a731be4c0b7cd9 100644 --- a/Lib/test/test_ttk/test_widgets.py +++ b/Lib/test/test_ttk/test_widgets.py @@ -125,7 +125,6 @@ def test_cb(arg1, **kw): def test_widget_cget(self): # Check whether the return value of cget is a string - self.assertIsInstance(self.widget.cget("width"), str) self.assertEqual(self.widget.cget("state"), tkinter.NORMAL) self.assertEqual(self.widget.cget("text"), "Text")