Skip to content

Commit bf03471

Browse files
j4321serhiy-storchaka
authored andcommitted
bpo-23831: Add moveto method to the tkinter.Canvas widget. (GH-9768)
1 parent dc0d571 commit bf03471

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

Doc/whatsnew/3.8.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ Added methods :meth:`~tkinter.Spinbox.selection_from`,
177177
in the :class:`tkinter.Spinbox` class.
178178
(Contributed by Juliette Monsel in :issue:`34829`.)
179179

180+
Added method :meth:`~tkinter.Canvas.moveto`
181+
in the :class:`tkinter.Canvas` class.
182+
(Contributed by Juliette Monsel in :issue:`23831`.)
183+
180184
venv
181185
----
182186

Lib/tkinter/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,15 @@ def move(self, *args):
29142914
"""Move an item TAGORID given in ARGS."""
29152915
self.tk.call((self._w, 'move') + args)
29162916

2917+
def moveto(self, tagOrId, x='', y=''):
2918+
"""Move the items given by TAGORID in the canvas coordinate
2919+
space so that the first coordinate pair of the bottommost
2920+
item with tag TAGORID is located at position (X,Y).
2921+
X and Y may be the empty string, in which case the
2922+
corresponding coordinate will be unchanged. All items matching
2923+
TAGORID remain in the same positions relative to each other."""
2924+
self.tk.call(self._w, 'moveto', tagOrId, x, y)
2925+
29172926
def postscript(self, cnf={}, **kw):
29182927
"""Print the contents of the canvas to a postscript
29192928
file. Valid options: colormap, colormode, file, fontmap,

Lib/tkinter/test/test_tkinter/test_widgets.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,29 @@ def test_yscrollincrement(self):
745745
self.checkPixelsParam(widget, 'yscrollincrement',
746746
10, 0, 11.2, 13.6, -10, '0.1i')
747747

748+
@requires_tcl(8, 6)
749+
def test_moveto(self):
750+
widget = self.create()
751+
i1 = widget.create_rectangle(1, 1, 20, 20, tags='group')
752+
i2 = widget.create_rectangle(30, 30, 50, 70, tags='group')
753+
x1, y1, _, _ = widget.bbox(i1)
754+
x2, y2, _, _ = widget.bbox(i2)
755+
widget.moveto('group', 200, 100)
756+
x1_2, y1_2, _, _ = widget.bbox(i1)
757+
x2_2, y2_2, _, _ = widget.bbox(i2)
758+
self.assertEqual(x1_2, 200)
759+
self.assertEqual(y1_2, 100)
760+
self.assertEqual(x2 - x1, x2_2 - x1_2)
761+
self.assertEqual(y2 - y1, y2_2 - y1_2)
762+
widget.tag_lower(i2, i1)
763+
widget.moveto('group', y=50)
764+
x1_3, y1_3, _, _ = widget.bbox(i1)
765+
x2_3, y2_3, _, _ = widget.bbox(i2)
766+
self.assertEqual(y2_3, 50)
767+
self.assertEqual(x2_3, x2_2)
768+
self.assertEqual(x2_2 - x1_2, x2_3 - x1_3)
769+
self.assertEqual(y2_2 - y1_2, y2_3 - y1_3)
770+
748771

749772
@add_standard_options(IntegerSizeTests, StandardOptionsTests)
750773
class ListboxTest(AbstractWidgetTest, unittest.TestCase):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add ``moveto()`` method to the ``tkinter.Canvas`` widget. Patch by Juliette
2+
Monsel.

0 commit comments

Comments
 (0)