Animation speed is increased when cursor is moving in app window using tkinter

Viewed 87

I am trying to do simple animation in Tkinter, just red box is moving pixel by pixel from one side to another. The problem is that when I move cursor on application window the animation has correct speed (33 ms between frames) but when I stop then animation slows down (to about 46 ms). I am using windows 11, IDE PyCharm and python 3.10.

Here is my code:

import time
import tkinter

root = tkinter.Tk()
canvas = tkinter.Canvas(root, height=1000, width=1000, bg='white')
canvas.pack()
box = canvas.create_rectangle(0, 475, 50, 525, fill='red')


def animation():
    global start
    print(str(int((time.time() - start)*1000)) + "ms")
    start = time.time()
    canvas.move(box, 1, 0)
    root.after(33, animation)


start = time.time()
animation()
root.mainloop()

After spending some time on searching forums for similar cases I found only one in which the solution was different version of python but I tried and it didn't work. Please help.

Thanks

0 Answers
Related