-->

Cannot delete tkinter object on canvas

2019-07-27 23:30发布

问题:

I have created a class object Dot(), a function update() that updates its position and then generated many instances of it on canvas, adding them to a list. I run a loop that keeps updating the position of the dots, and this works fine, but I also want to delete one instantiation of Dot() at each run of the loop.

I've tried many ways but with no success. By using canvas.delete() function I only "freeze" the instantiation, but it remains on screen. How can I fix this?

import numpy as np
import random
import time
from tkinter import *

#create Canvas#
screen = Tk()
hei = 600
wid = 600
canvas = Canvas(screen,width=wid,height=hei)
canvas.pack()

colors = ["red", "green", "blue"]


class Dot:
    def __init__(self):
        self.l = random.randrange(100,200)
        self.t = random.randrange(100,200)
        self.l2= self.l-10
        self.t2 = self.t-10
        color = random.choice(colors)
        self.shape = canvas.create_oval(self.l, self.t, self.l2, self.t2, fill=color)
        self.speedx = random.randrange(1,3)
        self.speedy = random.randrange(1,3)

    def update(self):
        canvas.move(self.shape, self.speedx, self.speedy)
        pos = canvas.coords(self.shape)
        jitter = random.randrange(0,200)
        if pos[2] >= wid+jitter or pos[0] <= -jitter:
            self.speedx *= -1
        if pos[3] >= hei+jitter or pos[1] <= -jitter:
            self.speedy *= -1


dots = []
for i in range(100):
    dots.append(Dot())

while True:
    del dots[-1]
    canvas.delete(dots[-1])
    for dot in dots:
        dot.update()
    screen.update()
    time.sleep(0.005)



screen.mainloop()

回答1:

The delete method takes the identifier of a canvas item, not the canvas item itself.

Additionally, you should probably do the deletion in a timeout function, otherwise, the GUI won't function properly.

Try this code:

import random
from tkinter import *

screen = Tk()
hei = 600
wid = 600
canvas = Canvas(screen, width=wid, height=hei)
canvas.pack()

colors = ["red", "green", "blue"]


class Dot:
    def __init__(self):
        self.l = random.randrange(100, 200)
        self.t = random.randrange(100, 200)
        self.l2 = self.l-10
        self.t2 = self.t-10
        color = random.choice(colors)
        self.shape = canvas.create_oval(self.l, self.t, self.l2, self.t2, fill=color)
        self.speedx = random.randrange(1, 3)
        self.speedy = random.randrange(1, 3)

    def update(self):
        canvas.move(self.shape, self.speedx, self.speedy)
        pos = canvas.coords(self.shape)
        jitter = random.randrange(0, 200)
        if pos[2] >= wid+jitter or pos[0] <= -jitter:
            self.speedx *= -1
        if pos[3] >= hei+jitter or pos[1] <= -jitter:
            self.speedy *= -1


dots = []
for i in range(100):
    dots.append(Dot())


def timeout():
    if dots:
        canvas.delete(dots[-1].shape)
        del dots[-1]
        for dot in dots:
            dot.update()
        screen.after(5, timeout)


screen.after(5, timeout)
screen.mainloop()

(Tested with Python 3)