Why does Tkinter erase previous rectangles when drawing?

When I draw a rectangle in global scope:

c = Canvas(width=IMAGE_WIDTH, height=IMAGE_HEIGHT, bg='black') 
c.create_rectangle([100, 100, 110, 110], fill='white')
c.pack()
root = Tk() 

and follow this by drawing a few rectangles in a loop

class gDrawer :
    def __init__(self) :
        self.rect_array = []
        self.x = 0
        self.y = 0

    def incr_counter(self,c,event=None): 
        one_pixel_loc = [self.x, self.y, self.x+5, self.y+5]
        self.rect_array.append(c.create_rectangle(one_pixel_loc, fill='white'))
        self.x += 1

gd = gDrawer()

for xx in range(100) :
    print xx
    gd.incr_counter(c)

root.mainloop()

The only drawn rectangle remains on the canvas, while the rectangular rectangle moves, rather than creating a trace of the rectangles. I would like to draw the trajectory of the rectangles, and not move, and what happens here?

+3
source share
1 answer

The default color for rectangleis black. This makes it seem like other rectangles are not drawn when their outline simply overlaps.

Try changing:

self.rect_array.append(c.create_rectangle(one_pixel_loc, fill='white'))

:

self.rect_array.append(c.create_rectangle(one_pixel_loc, fill='white', outline='white')

Not sure if this is exactly what you are looking for, hope this helps.

+4
source

All Articles