14

Consider:

from Tkinter import *


a = Tk()

canvas = Canvas(a, width = 500, height = 500)
canvas.pack()

canvas.create_rectangle(0, 0, 100, 100)

How do we delete this rectangle that's been created?

This is in reference to a game I am creating. It's a simple game where if the ball hits the block, the block should disappear. But if I do something like this:

class Block:
    def __init__(self,canvas,color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(10, 10, 110, 20, fill=color )
        self.id1 = canvas.create_rectangle(115, 10, 215, 20, fill=color)
        self.id2 = canvas.create_rectangle(220, 10, 320, 20, fill=color)
        self.id3 = canvas.create_rectangle(325, 10, 425, 20, fill=color)
        self.id4 = canvas.create_rectangle(430, 10, 530, 20, fill=color)
        self.id5 = canvas.create_rectangle(100, 150, 200, 160, fill=color)
        self.id6 = canvas.create_rectangle(350, 150, 450, 160, fill=color)
        self.x = 0

And then:

    def hit_block(self,pos):
        block_pos = self.canvas.coords(self.block.id)
        List = [block_pos]
        for i in List:
            if pos[0] >= i[0] and pos[2] <= i[2]:
                if pos[1] >= i[1] and pos[1] <= i[3]:
                    canvas.delete(block.id)
                    self.score()
                    global a
                    a += 1
                    return True
        return False

It doesn't work. How can I delete the block when the ball hits it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Avinash12388
  • 1,092
  • 1
  • 9
  • 19
  • 2
    http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.delete-method – atlasologist May 16 '14 at 00:58
  • 1
    Have you read the documentation? How to do this is clearly documented. If not, why not? If so, did you not understand part of it? If that's the case, tell us what part you don't understand so we can focus our help. – Bryan Oakley May 16 '14 at 00:58
  • 1
    This question appears to be off-topic because it can be solved by reading the official documentation – M4rtini May 19 '14 at 08:37
  • When I try to delete like the above answer, can't delete the border. Instead of that, I rather use canvas.delete("all") https://stackoverflow.com/questions/15839491/how-to-clear-tkinter-canvas – Yonghwan Shin Sep 01 '19 at 06:15

2 Answers2

27

Assign the create_rectangle() to a variable, and then call canvas.delete() on that variable:

from Tkinter import *


a = Tk()

canvas = Canvas(a, width = 500, height = 500)
canvas.pack()

myrect = canvas.create_rectangle(0,0,100,100)
canvas.delete(myrect) #Deletes the rectangle

Window before deletion:

Picture before deletion

Window after deletion:

Picture after deletion

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
  • Thanks, and that was something I was looking for, but now that Ive edited my question, could you help me out there as well? – Avinash12388 May 16 '14 at 15:13
3

In my opinion better option is add a Option tags= to function create_rectangle() and u can avoid creating new variables.

from Tkinter import *
a = Tk()
canvas = Canvas(a, width = 500, height = 500)
canvas.pack()

canvas.create_rectangle(0,0,100,100, tags="square")
canvas.delete("square") #Deletes the rectangle wchich have tags option named "square"

myrect = canvas.create_rectangle(0,0,100,100)

Btw. it's a problem when u "delete" an object from myrect to "create" them again in the same variable.

Frogen10
  • 31
  • 2