Tkinter 删除定义坐标处的所有移动对象
Tkinter delete all moving objects at defined coords
我有一些形状和一堆(数千条)移动线。每次都会创建线条并向下移动。如果它们的最高坐标是 200,我想删除它们。但是当我尝试这个时,所有 canvas 停止移动(其他形状和所有线停止并且所有线被删除)。我在这里需要帮助。我想要的是:线条将在某些 X 坐标处被删除,同时线条的创建和移动将继续动态进行,而不会停止或关闭任何进程。
class Flow:
def __init__(self, tph, color, canvas):
self.flows = []
self.tph = tph
self.color = color
self.Xspeed = 10
self.Yspeed = 0
self.canvas = canvas
def create_flow(self):
for i in range(self.tph):
self.flow = self.canvas.create_line(550, 50, 500, 52, fill=self.color, width=2)
self.flows.append(self.flow)
return self.flows
def move_flow(self):
for self.flow in self.flows:
self.canvas.move(self.flow, self.Xspeed - randint(10, 30), self.Yspeed + randint(10, 20))
flow = Flow(10, "#976C3D", canvas)
def animate():
flow.create_flow()
flow.move_flow()
canvas.after(100, animate)
animate()
tk.mainloop()
如果要删除y坐标> 200的线,可以修改move_flow()
如下:
def move_flow(self):
for flow in self.flows:
coords = self.canvas.coords(flow)
if coords[1] > 200: # top y > 200
self.canvas.delete(flow) # delete the canvas line item
self.flows.remove(flow) # delete the item in self.flows as well
else:
self.canvas.move(flow, self.Xspeed-randint(10, 30), self.Yspeed+randint(10, 20))
我有一些形状和一堆(数千条)移动线。每次都会创建线条并向下移动。如果它们的最高坐标是 200,我想删除它们。但是当我尝试这个时,所有 canvas 停止移动(其他形状和所有线停止并且所有线被删除)。我在这里需要帮助。我想要的是:线条将在某些 X 坐标处被删除,同时线条的创建和移动将继续动态进行,而不会停止或关闭任何进程。
class Flow:
def __init__(self, tph, color, canvas):
self.flows = []
self.tph = tph
self.color = color
self.Xspeed = 10
self.Yspeed = 0
self.canvas = canvas
def create_flow(self):
for i in range(self.tph):
self.flow = self.canvas.create_line(550, 50, 500, 52, fill=self.color, width=2)
self.flows.append(self.flow)
return self.flows
def move_flow(self):
for self.flow in self.flows:
self.canvas.move(self.flow, self.Xspeed - randint(10, 30), self.Yspeed + randint(10, 20))
flow = Flow(10, "#976C3D", canvas)
def animate():
flow.create_flow()
flow.move_flow()
canvas.after(100, animate)
animate()
tk.mainloop()
如果要删除y坐标> 200的线,可以修改move_flow()
如下:
def move_flow(self):
for flow in self.flows:
coords = self.canvas.coords(flow)
if coords[1] > 200: # top y > 200
self.canvas.delete(flow) # delete the canvas line item
self.flows.remove(flow) # delete the item in self.flows as well
else:
self.canvas.move(flow, self.Xspeed-randint(10, 30), self.Yspeed+randint(10, 20))