Skip to content
Advertisement

Python tkinter after method not working as expected

I’m trying to learn Tkinter module, but I can’t undestand why the after method doesn’t behave as expected. From what I know, it should wait ms milliseconds and then execute the function, but in my case the function gets executed many more time, not considering the time I write. Here’s the code:

JavaScript

After the first 2 seconds the label starts displaying humongous numbers

Advertisement

Answer

After the first 2 seconds the label starts displaying humongous numbers

Keep in mind, while True, is an infinite loop, you are making infinite calls to root.after() means alot of events are being scheduled to be called after 1 second. Better way to do this is to remove your while and move it all inside your function.

JavaScript

Though the best way to write the function would be to create a variable and increase the value of that variable inside the function and then show it out:

JavaScript

This way you can reduce the complexity of your code too and make use of the variable effectively and avoid nasty type castings ;)

Advertisement