How it works...

The canvas.delete() method takes one argument, which can be an item identifier or a tag, and removes the matching item or items, since the same tag can be used multiple times.

In the on_click() handler, we can see an example of how to remove an item by its identifier:

    def on_click(self, event):
item = self.canvas.find_withtag(tk.CURRENT)
self.canvas.delete(item)

Note that if we click on an empty point, canvas.find_withtag(tk.CURRENT) will return None, but it will not raise any error when passed to canvas.delete(). This happens because the None parameter will not match any item identifier or tag and therefore, it is a valid value even though it will not perform any action.

In the clear_items() callback, we can find another example of deleting items. Here, instead of passing an item identifier, we used the ALL tag to match all the items and remove them from the canvas:

    def clear_items(self):
self.canvas.delete(tk.ALL)

As you may have noticed, the ALL tag works out of the box and does not need to be added to every canvas item.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset