Leero11 Leero11 - 3 months ago 11
Python Question

Python/Tkinter - delete all objects in enclosed area

I am trying to make a program where on click the program would delete all objects in enclosed area.

Here is my example code:

import tkinter as tk

root = tk.Tk()

cv = tk.Canvas(root, height=400, width=400)
cv.pack()

cv.create_rectangle(50, 50, 100, 100)

cv.create_line(60, 60, 80, 80)

cv.create_line(60, 80, 80, 60)

def onclick():

todel = cv.find_enclosed(50, 50, 100, 100)
cv.delete(todel)

cv.bind("<Button-1>", onclick())

root.mainloop()


On click it should delete the two lines in the rectangle, but for some reason it does not. How can I make this happen?

Answer

You have to apply delete to all items of the list

for d in todel:
    cv.delete(d)

or

any(map(cv.delete,todel))
Comments