10000 gh-43457: Tkinter: fix design flaws in wm_attributes() by serhiy-storchaka · Pull Request #111404 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-43457: Tkinter: fix design flaws in wm_attributes() #111404

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add optional return_python_dict argument.
  • Loading branch information
serhiy-storchaka committed Nov 14, 2023
commit 4852ecd6c73369435cfdecd74bee8ec319ec33dd
9 changes: 3 additions & 6 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ tkinter
the attribute name without the minus prefix to get window attributes,
e.g. ``w.wm_attributes('alpha')`` and allows to specify attributes and
values to set as keyword arguments, e.g. ``w.wm_attributes(alpha=0.5)``.
Add new optional keyword argument *return_python_dict*: calling
``w.wm_attributes(return_python_dict=True)`` returns the attributes as
a dict instead of a tuple or string.
(Contributed by Serhiy Storchaka in :gh:`43457`.)

traceback
Expand Down Expand Up @@ -1014,12 +1017,6 @@ Changes in the Python API
The result is now the same if ``wantobjects`` is set to ``0``.
(Contributed by Serhiy Storchaka in :gh:`97928`.)

* The :mod:`tkinter` widget method :meth:`!wm_attributes` called without
arguments now returns a dict instead of a tuple or a str.
E.g. ``{'alpha': 1.0, 'topmost': 0, 'zoomed': 0, 'fullscreen': 0, 'type': ''}``
instead of ``('-alpha', 1.0, '-topmost', 0, '-zoomed', 0, '-fullscreen', 0, '-type', '')``.
(Contributed by Serhiy Storchaka in :gh:`43457`.)

* Functions :c:func:`PyDict_GetItem`, :c:func:`PyDict_GetItemString`,
:c:func:`PyMapping_HasKey`, :c:func:`PyMapping_HasKeyString`,
:c:func:`PyObject_HasAttr`, :c:func:`PyObject_HasAttrString`, and
Expand Down
7 changes: 5 additions & 2 deletions Lib/test/test_tkinter/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,14 @@ class WmTest(AbstractTkTest, unittest.TestCase):

def test_wm_attribute(self):
w = self.root
attributes = w.wm_attributes()
attributes = w.wm_attributes(return_python_dict=True)
self.assertIsInstance(attributes, dict)
attributes2 = w.wm_attributes()
self.assertIsInstance(attributes2, tuple if self.wantobjects else str)
# silently deprecated
attributes2 = w.wm_attributes(None)
attributes3 = w.wm_attributes(None)
self.assertIsInstance(attributes2, tuple if self.wantobjects else str)
self.assertEqual(attributes3, attributes2)
if self.wantobjects:
self.assertEqual(attributes2[::2],
tuple('-' + k for k in attributes))
Expand Down
27 changes: 18 additions & 9 deletions Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2089,18 +2089,27 @@ def wm_aspect(self,

aspect = wm_aspect

def wm_attributes(self, *args, **kwargs):
"""This subcommand returns or sets platform specific attributes

When called without arguments, return a dict of the platform
specific attributes and their values. When called with a single
string value, return the value for the specific option. When
called with keyword arguments, set the corresponding attributes.
def wm_attributes(self, *args, return_python_dict=False, **kwargs):
"""Return or sets platform specific attributes.

When called without arguments and return_python_dict is true,
return a dict of the platform specific attributes and their values.
If return_python_dict is false (default) and wantobjects is true,
return a tuple containing intermixed atrribute names with the minus
prefix and their values. If return_python_dict is false and
wantobjects is false, return a string representing the
corresponding Tcl list.

When called with a single string value, return the value for the
specific option. When called with keyword arguments, set the
corresponding attributes.
"""
if not kwargs:
if not args:
return _splitdict(self.tk,
self.tk.call('wm', 'attributes', self._w))
res = self.tk.call('wm', 'attributes', self._w)
if return_python_dict:
res = _splitdict(self.tk, res)
return res
if len(args) == 1 and args[0] is not None:
option = args[0]
if option[0] == '-':
Expand Down
0