【发布时间】:2012-12-31 17:35:37
【问题描述】:
我在从类中删除画布对象时遇到问题。
我创建了一个名为 f 的 Rectangle 类型的对象。然后我需要删除这个对象。 Python 会删除f,但不会删除 Frame 上的画布对象。我不知道问题出在哪里。
from tkinter import *
class Rectangle():
def __init__(self, coords, color):
self.coords = coords
self.color = color
def __del__(self):
print("In DELETE")
del self
print("Goodbye")
def draw(self, canvas):
"""Draw the rectangle on a Tk Canvas."""
print("In draw ")
print("Canvas = ",canvas)
print("self = ",self)
print("bild canvas = ",canvas.create_rectangle(*self.coords, fill=self.color))
root = Tk()
root.title('Basic Tkinter straight line')
w = Canvas(root, width=500, height=500)
f = []
f = Rectangle((0+30*10, 0+30*10, 100+30*10, 100+30*10), "yellow")
print("Draw object", f.draw(w), f)
f.__del__()
del f
w.pack()
mainloop()
【问题讨论】:
-
Canvas 对象被分配给引用 w。那是您要删除的内容吗?
-
是的,如果我这样做 w.delete(f) 什么都没有发生