"_tkinter.TclError: bad screen distance" in python's tkinter when trying to modify object coordinates with Canvas.coords()

"_tkinter.TclError: bad screen distance" in python's tkinter when trying to modify object coordinates with Canvas.coords()

我正在尝试用一些可以移动和旋转的项目制作一个 canvas,为此,我有修改坐标的功能,但是我在移动对象时遇到了问题。我正在尝试使用 coords 函数来更改每个对象的坐标。

当前引发错误的代码位是:

count = 1
for part in self._createdpartlist:
    self.coords(part, self._partlist[count].coordinates)
    count += 1

self 是我创建的 Canvas 对象。 createdpartlist 包含在 canvas(所有 4 边多边形)中创建的零件的 id,partlist 是一个对象列表,这些对象的坐标以 [(x1, y1), (x2, y2), ( x3, y3), (x4, y4)]

然而,当我尝试 运行 它时,我得到了错误;

_tkinter.TclError: bad screen distance "340)]" 

(在本例中 340 是 y4 坐标)

我不完全知道屏幕距离差是什么意思,也无法真正弄清楚出了什么问题或者我是否错误地使用了坐标函数。

非常感谢任何帮助

编辑:当我创建一个仅包含此文件的新文件时出现此错误。

from tkinter import *

coordinates = [(330,230), (350,230), (350,340), (330,340)]
new_coords = [(340,245), (340,260), (400,260), (400,245)]

c = Canvas()

shape = c.create_polygon(coordinates)

c.coords(shape, new_coords)

在这种情况下错误出现为“245)]”而不是“340)]”

你能试试这个吗?等我不在手机上的时候再试试

import itertools
try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk


# from itertools recipes: https://docs.python.org/2/library/itertools.html
def flatten(list_of_lists):
    """Flatten one level of nesting"""
    return itertools.chain.from_iterable(list_of_lists)


coordinates = [(330,230), (350,230), (350,340), (330,340)]
new_coords = [(340,245), (340,260), (400,260), (400,245)]
c = tk.Canvas()
shape = c.create_polygon(coordinates)
c.coords(shape, *flatten(new_coords))

如果可行,请尝试:

for i, part in enumerate(self._createdpartlist):
    self.coords(part, *flatten(self._partlist[i+1].coordinates))

我在尝试 Pypy3 5.5.0-alpha 时 运行 进入 _tkinter.TclError: bad screen distance

将坐标列表更改为元组很有帮助。