I want to change the color of rectangle after a certain period of time.
I tried root.after() method but it is not working.
import time
from tkinter import *
def task():
canvas= Canvas()
canvas.create_rectangle(0,0,100,100,fill='red')
canvas.pack()
time.sleep(2)
canvas.create_rectangle(0,0,100,100,fill='blue')
canvas.pack()
time.sleep(2)
print("Testing...")
root = Tk()
canvas = Canvas(root)
while(1):
task()
root.mainloop()
Given code running continuously and root windows get hang for certain time. Rectangle color should change after 2 seconds of delay.
task()will be executed only once becauseroot.mainloop()will block the while loop. You should useafter(). What have you tried usingafter()? - acw1668