Generate 10 balls in tkinter canvas
Question: Ok guys. I am trying to generate 10 balls of random color in Tkinter canvas
when I click the generate button. Program works, and random color choice works
for the ball, but I only get one ball generated at a time. Every time I click
the button it randomly moves the ball around, but all I want is 10 balls in 10
random positions at a time. I am using Python 3.4 on a Linux box. This is a
code I've got:

    
    
    from tkinter import *
    import random # to generate random balls
    
    colors = ["red", "blue", "purple", "green", "violet", "black"]
    
    class RandomBalls:
        """
        Boilerplate code for window in Tkinter
        window = Tk()
        window.title("Random title")
        window.mainloop()
        """
    
    
        def __init__(self):
            """
            Initialize the window and add two frames, one with button, and another one with
            canvas
            :return:
            """
    
            window = Tk()
            window.title("Random balls")
    
            # A canvas frame
            frame1 = Frame(window)
            frame1.pack()
            self.canvas = Canvas(frame1, width = 200, height = 300, bg = "white")
            self.canvas.pack()
    
    
            # A button frame
            frame2 = Frame(window)
            frame2.pack()
            displayBtn = Button(frame2, text = "Display", command = self.display)
            displayBtn.pack()
    
            window.mainloop()
    
        def display(self):
            for i in range(0, 10):
                self.canvas.delete("circle")  # delete references to the old circle
                self.x1 = random.randrange(150)
                self.y1 = random.randrange(200)
                self.x2 = self.x1 + 5
                self.y2 = self.y1 + 5
                self.coords = self.x1, self.y1, self.x2, self.y2
                self.canvas.create_oval(self.coords, fill = random.choice(colors), tags = "circle")
            self.canvas.update()
    
    
    RandomBalls()
    


Answer: Every time through your loop you are deleting everything you created before,
including what you created the previous iteration. Move the delete statement
outside of the loop:

    
    
    def display(self):
        self.canvas.delete("circle")
        for i in range(0, 10):
            ...
    

