Hey i am new to python and i am trying to update my GUI every 3 seconds 4 times. I have tried time.sleep() but it doesnt work. Any ideas?

for i in range(4):
    #creates 10 circles randomly on the canvas
    for _ in range(10):
         circle1= Circle(canvas, random.randint(1,750), random.uniform(1,450), "red")

GUI's have their own "sleep" type methods that should be used. However each GUI uses a different function name so it depends on the GUI toolkit you are using.

I have been using tkinter and messing around with the after() but am having difficulties

Tkinter does not have a Circle class; you use create_oval with circle dimensions. Note that you call the function once and have it call itself recursively for as many times as you want.

    import Tkinter as tk
    import tkinter as tk

class TestAfter():
    def __init__(self):
        self.ctr = 3
        self.canvas = tk.Canvas(self.root, width=500, height=500)
        self.root.after(750, self.update)


    def update(self):
        colors = ["black", "red", "green", "blue"]

        if self.ctr > 0:
            start = 50*self.ctr  ## use same values for x & y
            end = start+50
            self.canvas.create_oval(start, start, end, end,
                               fill=colors[self.ctr], width=2)
            self.ctr -= 1
            self.root.after(750, self.update)


Edited 3 Years Ago by woooee

This article has been dead for over six months. Start a new discussion instead.