[Python-checkins] gh-94473: Flatten arguments in tkinter.Canvas.coords() (GH-98479)

serhiy-storchaka webhook-mailer at python.org
Mon May 22 04:55:27 EDT 2023


https://github.com/python/cpython/commit/9bc80dac47f6d43d0bbfbf10c4cc3848b175e97f
commit: 9bc80dac47f6d43d0bbfbf10c4cc3848b175e97f
branch: main
author: Serhiy Storchaka <storchaka at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2023-05-22T11:54:41+03:00
summary:

gh-94473: Flatten arguments in tkinter.Canvas.coords() (GH-98479)

It now accepts not only "x1, y1, x2, y2, ..." and "[x1, y1, x2, y2, ...]",
but also "(x1, y1), (x2, y2), ..." and "[(x1, y1), (x2, y2), ...]".

files:
A Misc/NEWS.d/next/Library/2022-10-20-14-03-58.gh-issue-94473.pzGX73.rst
M Doc/whatsnew/3.12.rst
M Lib/test/test_tkinter/test_widgets.py
M Lib/tkinter/__init__.py

diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst
index caf21078b9bd..efbd2ca3de12 100644
--- a/Doc/whatsnew/3.12.rst
+++ b/Doc/whatsnew/3.12.rst
@@ -532,6 +532,17 @@ threading
   profiling functions in all running threads in addition to the calling one.
   (Contributed by Pablo Galindo in :gh:`93503`.)
 
+tkinter
+-------
+
+* ``tkinter.Canvas.coords()`` now flattens its arguments.
+  It now accepts not only coordinates as separate arguments
+  (``x1, y1, x2, y2, ...``) and a sequence of coordinates
+  (``[x1, y1, x2, y2, ...]``), but also coordinates grouped in pairs
+  (``(x1, y1), (x2, y2), ...`` and ``[(x1, y1), (x2, y2), ...]``),
+  like ``create_*()`` methods.
+  (Contributed by Serhiy Storchaka in :gh:`94473`.)
+
 types
 -----
 
diff --git a/Lib/test/test_tkinter/test_widgets.py b/Lib/test/test_tkinter/test_widgets.py
index b45245162f24..76cc16e5b977 100644
--- a/Lib/test/test_tkinter/test_widgets.py
+++ b/Lib/test/test_tkinter/test_widgets.py
@@ -901,6 +901,12 @@ def test_coords(self):
         c.coords(i, [21, 31, 41, 51, 61, 11])
         self.assertEqual(c.coords(i), [21.0, 31.0, 41.0, 51.0, 61.0, 11.0])
 
+        c.coords(i, (22, 32), (42, 52), (62, 12))
+        self.assertEqual(c.coords(i), [22.0, 32.0, 42.0, 52.0, 62.0, 12.0])
+
+        c.coords(i, [(23, 33), (43, 53), (63, 13)])
+        self.assertEqual(c.coords(i), [23.0, 33.0, 43.0, 53.0, 63.0, 13.0])
+
         c.coords(i, 20, 30, 60, 10)
         self.assertEqual(c.coords(i), [20.0, 30.0, 60.0, 10.0])
         self.assertEqual(c.bbox(i), (18, 8, 62, 32))
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index bf0b3b921559..c675c511e045 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -2817,7 +2817,7 @@ def canvasy(self, screeny, gridspacing=None):
 
     def coords(self, *args):
         """Return a list of coordinates for the item given in ARGS."""
-        # XXX Should use _flatten on args
+        args = _flatten(args)
         return [self.tk.getdouble(x) for x in
                            self.tk.splitlist(
                    self.tk.call((self._w, 'coords') + args))]
diff --git a/Misc/NEWS.d/next/Library/2022-10-20-14-03-58.gh-issue-94473.pzGX73.rst b/Misc/NEWS.d/next/Library/2022-10-20-14-03-58.gh-issue-94473.pzGX73.rst
new file mode 100644
index 000000000000..f70722a6f0f6
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-10-20-14-03-58.gh-issue-94473.pzGX73.rst
@@ -0,0 +1,3 @@
+Flatten arguments in :meth:`tkinter.Canvas.coords`. It now accepts not only
+``x1, y1, x2, y2, ...`` and ``[x1, y1, x2, y2, ...]``, but also ``(x1, y1),
+(x2, y2), ...`` and ``[(x1, y1), (x2, y2), ...]``.



More information about the Python-checkins mailing list