Skip to content

gh-126008: Fix the type of return value for ttk.Widget.cget #126102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
5 changes: 5 additions & 0 deletions Lib/test/test_ttk/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ 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.assertEqual(self.widget.cget("state"), tkinter.NORMAL)
self.assertEqual(self.widget.cget("text"), "Text")


class AbstractToplevelTest(AbstractWidgetTest, PixelSizeTests):
_rounds_pixels = False
Expand Down
11 changes: 11 additions & 0 deletions Lib/tkinter/ttk.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"tclobjs_to_py", "setup_master"]

import tkinter
import _tkinter
from tkinter import _flatten, _join, _stringify, _splitdict


Expand Down Expand Up @@ -572,6 +573,16 @@ 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_value = super().cget(key)
if isinstance(return_value, _tkinter.Tcl_Obj):
return str(return_value)
return return_value

__getitem__ = cget


class Button(Widget):
"""Ttk Button widget, displays a textual label and/or image, and
evaluates a command when pressed."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix an incorrect return value type when getting resource value of ttk widgets.
Loading