I am having difficulty understanding how to delete an object from the canvas. A rocket object is created from Class Rocket and is just a simple oval. After an event, I'm trying to use the canvas.delete() method with the argument set to rocket in order to remove this object from the canvas but it doesn't seem to work.
My goal is to create multiple rockets in a list and delete some during the animation.
I have include just main() function. The Class Rocket just creates an oval on the canvas and works fine.
file: constants.py
CANVAS_WIDTH = 600 # Width of drawing canvas in pixels
CANVAS_HEIGHT = 600 # Height of drawing canvas in pixels
ROCKET_SIZE = 30
file: test.py
import tkinter
import time
import random
from constants import *
from Rocket import Rocket
def main():
canvas = make_canvas(CANVAS_WIDTH, CANVAS_HEIGHT, 'Fireworks')
#put a rocket a bottom of canvas
rocket = Rocket(canvas)
canvas.update()
time.sleep(50/50.) #pause to see the rocket
# some event occurs (not important for this example)
canvas.delete(rocket) #this doesn't make the rocket disappear!
canvas.update()
time.sleep(50/50.)
def make_canvas(width, height, title):
top = tkinter.Tk()
top.minsize(width=width, height=height)
top.title(title)
canvas = tkinter.Canvas(top, width=width + 1, height=height + 1)
canvas.pack()
return canvas
if __name__ == '__main__':
main()
file: Rocket.py
import random
from constants import *
class Rocket:
'''
This is the blueprint for a new variable type called "Rocket"
Every rocket has three things: an oval, a change_x and a change_y.
Every rocket supports the "update" method which will move the rocket one step.
'''
def __init__(self, canvas):
# Starting point - screen bottom with slight angle
x_1 = random.randint(CANVAS_WIDTH/2 - 50, CANVAS_WIDTH/2 + 50)
y_1 = CANVAS_HEIGHT - ROCKET_SIZE
x_2 = x_1 + ROCKET_SIZE
y_2 = CANVAS_HEIGHT
self.fill = random.choice(['blue', 'green', 'orange', 'purple', 'red', 'lime'])
self.oval = canvas.create_oval(x_1, y_1, x_2, y_2, fill=self.fill, outline=self.fill)
self.change_x = random.randint(-3, 3)
self.change_y = random.randint(-15, -5)
# again, I pass in canvas.
def update(self, canvas):
# update a single rocket instance (the one given by self)
canvas.move(self.oval, self.change_x, self.change_y)