Here we are providing an exciting tutorial in Tkinter on how to create an animation in a Tkinter.

Sample code

The following Tkinter code will demonstrate the same:

from tkinter import *
import time

root = Tk()
canvas = Canvas(root, width=300, height=300)
canvas.pack()
canvas.create_polygon(10,10,10,60,50,35)

for i in range(0,60):
    canvas.move(1,5,0)
    root.update()
    time.sleep(0.1)

root.mainloop()


In the above code, a canvas and a polygon is created first, then a for-loop is run from 0 to 60, under the definition, move is used to move the polygon with id=1 in x-direction by 5 and in y-direction by 1, these are the 3 parameters that are passed to the move method. Then root is updated using update method, we also used sleep method from time class that we imported from import time line.

Output

output