0

How can I delete a specific image tag of a canvas with canvas.delete()? Should I put 'img' as a parameter or is something else required?

ball = PhotoImage(file='ball.png')
[Class Name and __init__]
self.canvas.delete('img')
self.canvas.create_image(self.getCoordinates()[0], self.getCoordinates()[1], image=ball)

2 Answers2

2

Creating a canvas object returns an id. You can pass that id to the delete method.

self.image_id = self.canvas.create_image(...)
...
self.canvas.delete(self.image_id)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
1

Yes, to remove an item with a tag, first you need to assign the tag with the tag option for tk.Canvas.create_xxxxx(...,tag='item1'):

self.canvas.create_image(self.getCoordinates()[0], self.getCoordinates()[1], image=ball, tag='img')
Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46