0

当我想关闭应用程序时,我收到以下错误:

Exception _tkinter.TclError: 'invalid command name ".47045488"' in <bound method
RootJoint.__del__ of <skeleton.RootJoint object at 0x0000000002CDDBA8>> ignored

这是来自的相关部分RootJoint

def __init__(self, canvas):
    self.parent = None
    self.angle = 0.0
    self.distance = 0.0

    topleft = 0 - Joint.SQUARE_HALF_SIDE, 0 + Joint.SQUARE_HALF_SIDE
    bottomright = 0 + Joint.SQUARE_HALF_SIDE, 0 - Joint.SQUARE_HALF_SIDE

    self._joint = canvas.create_rectangle(*(topleft + bottomright))
    canvas.itemconfig(self._joint, fill=RootJoint.COLOR_NORMAL)

    self.canvas = canvas

def __del__(self):
    self.canvas.delete(self._joint)

这是创建画布和根关节的地方:

def main():
    root = tk.Tk()
    root.geometry('640x480')

    canvas = tk.Canvas(root)
    canvas.pack(fill=tk.BOTH, expand=True)

    rootjoint = skeleton.RootJoint(canvas)

    root.mainloop()

为什么会出现此错误,我该如何解决?

4

1 回答 1

1

问题是

德尔

当对canves的最后一个引用丢失并且对象被删除时调用。

你想要的是

def __init__(...):
    ...
    self.bind("<Destroy>", self.onDestroy)
def onDestroy(self, event):
    self.canvas.delete(self._joint)

首先画布和所有元素将被销毁和破坏,然后删除

于 2012-05-12T20:04:00.393 回答