我想创建,删除,然后-再次-在画布上创建项目。如果新创建的项目具有与以前相同的ID,它将非常方便。
我试图通过删除带有canvas.delete(tag)
的项目并同时删除带有canvas.dtag(dot, ID-of-dot)
的项目来处理此问题。到目前为止,注意工作。这是一个例子,说明我的意思:
import tkinter as tk
root = tk.Tk()
x = 0
y = 25
def oval():
global x, y
coords = [x-5, y-5, x+5, y+5]
if x < 40:
x += 10
else:
x = 10
canv.delete("dots")
coords = [x - 5, y - 5, x + 5, y + 5]
canv.create_oval(*coords, tags="dots")
dot_list = canv.find_withtag("dots")
txt.delete(1.0, "end")
txt.insert(0.0, dot_list)
canv = tk.Canvas(root, width=50, height=50)
canv.grid(row=0, column=0)
but1 = tk.Button(root, width=10, height=1, text="Push Me", command=oval)
but1.grid(row=0, column=1)
txt = tk.Text(width=10, height=1)
txt.grid(row=0, column=2)
root.mainloop()
我想重复使用相同的ID(1、2、3、4),而不是像这里一样继续计数。有什么办法吗?
您不能重复使用画布项目ID。