Reputation: 59
for i in range(random.randint(1,4)):
xos=[150,200,250,300,350,400,450,500,550,600,650,700,750,800,850]
yos=[150,200,250,300,350,400,450,500,550,600,650]
xos_=random.choice(xos)
yos_=random.choice(yos)
object=canvas.create_image(xos_,yos_,image=postava)
read_=read.replace("[","").replace("]","").replace("'","").replace("\\n","").replace("\\","")
loot.write(read_+"\n")
I am creating images on canvas in FOR and i want to remove them when I need, but only the last one has the tag (name) object so when I type: canvas.delete(object)
it deletes only the last one. So I want to know if it is possible to delete an object which is at certain position (without a name or tag).
Upvotes: 1
Views: 2995
Reputation: 8234
Sure. Scenario (A) Let's say we use the mouse left button to identify the canvas object when clicked and delete the object when mouse left button is released.
Step 1: Include these commands to the callback/method that you use to bind to Button-1
to delete an object.
mx = canvas.canvasx(event.x) #Translate mouse x screen coordinate to canvas coordinate
my = canvas.canvasy(event.y) #Translate mouse y screen coordinate to canvas coordinate
self.canvasobject = canvas.find_closest(mx, my, halo=5) # get canvas object ID of where mouse pointer is
print(self.canvasobject) #For you to visualize the canvas object number
Step 2: Include these commands to the callback/method that you use to bind to ButtonRelease-1
to delete an object.
canvas.delete(self.canvasobject) #delete the selected canvas object
Scenario (B): Assume you already know the object canvas x,y coordinates, you can issue a single command to delete the canvas object:
canvas.delete(canvas.find_closest(x, y, halo=5))
See this webpage for explanation on the canvas methods that I have used and for other canvas methods.
Upvotes: 1
Reputation: 983
It is possible to delete an object on the canvas if you know the coordinates (coord). Use item = canvas.find_overlapping(coord)
to find the object at that location, then delete it using canvas.delete(item)
See sample code below:
Note: if you know just a single point on the object, then use canvas.find_overlapping(x, y, x, y)
instead of canvas.find_overlapping(x1, y1, x2, y2)
import Tkinter as tk
import random
root = tk.Tk()
canvas = tk.Canvas(root, width=550, height=500, borderwidth=0)
canvas.pack(expand=True, fill="both")
coord_list=[]
for i in range(random.randint(1,4)):
xos=[150,200,250,300,350,400,450,500]
yos=[150,200,250,300,350,400,450]
xos_=random.choice(xos)
yos_=random.choice(yos)
coord = (xos_,yos_,xos_+50,yos_+50)
coord_list.append(coord)
objectt=canvas.create_rectangle(coord, fill="blue")
canvas.create_rectangle(25, 15, 50, 40, fill="red")
# Delete red rectangle
def delete1(event):
item = canvas.find_overlapping(25, 15, 50, 40)
canvas.delete(item)
# Delete blue rectangles
def delete2(event):
for coord in coord_list:
item = canvas.find_overlapping(*coord)
canvas.delete(item)
#Click on the canvas to delete objects at the coordinates
canvas.bind("<Button-1>", delete1) # change function to delete blue rectangles
root.mainloop()
Upvotes: 1