[英]"_tkinter.TclError: bad screen distance" in python's tkinter when trying to modify object coordinates with Canvas.coords()
I am trying to make a canvas with some items that can move and rotate, to do this, i have functions to modify the coordinates, however i am having trouble with moving the objects.我正在尝试使用一些可以移动和旋转的项目制作画布,为此,我有修改坐标的功能,但是我在移动对象时遇到了麻烦。 I am trying to use the coords function to change the coordinates of each object.
我正在尝试使用 coords 函数来更改每个对象的坐标。
the current bit of code that is raising the error is:引发错误的当前代码位是:
count = 1
for part in self._createdpartlist:
self.coords(part, self._partlist[count].coordinates)
count += 1
self is a Canvas object i created. self 是我创建的 Canvas 对象。 with createdpartlist containing id's of created parts in canvas (all 4 sided polygons) and partlist being a list of objects that have coordinates that are returned in the form of [(x1, y1), (x2, y2), (x3, y3), (x4, y4)]
createdpartlist 包含画布中创建部件的 id(所有 4 个边多边形),partlist 是具有以 [(x1, y1), (x2, y2), (x3, y3) 形式返回的坐标的对象列表, (x4, y4)]
however when i try to run it, i get the error;但是,当我尝试运行它时,出现错误;
_tkinter.TclError: bad screen distance "340)]"
(in this case 340 is the y4 coordinate) (在这种情况下 340 是 y4 坐标)
I dont exactly know what it means by bad screen distance, and cant really figure out whats going wrong or if i am using coords function incorrectly.我不完全知道糟糕的屏幕距离是什么意思,也无法真正弄清楚出了什么问题,或者我是否错误地使用了坐标函数。
Any help is greatly appreciated任何帮助是极大的赞赏
Edit: i get this error when i make a new file only containing this.编辑:当我创建一个只包含这个的新文件时,我收到这个错误。
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)
the error comes up with "245)]" instead of "340)]" in this instance在这种情况下,错误出现在“245)]”而不是“340)]”
Can you try this?你能试试这个吗? I will try it later when I am not on mobile.
稍后我不在手机上时会尝试一下。
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))
If that works then try:如果可行,请尝试:
for i, part in enumerate(self._createdpartlist):
self.coords(part, *flatten(self._partlist[i+1].coordinates))
I ran into _tkinter.TclError: bad screen distance
when trying Pypy3 5.5.0-alpha.我在尝试 Pypy3 5.5.0-alpha 时遇到了
_tkinter.TclError: bad screen distance
。
Changing coordinate lists into tuples helped.将坐标列表更改为元组有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.