-1

I'm trying to make a simple coutdown timer that will count down from 15 seconds and update the window by writing the second we are on at the moment. This is my code.

from tkinter import *
import time

def Timer():
    time_left = 15
    while time_left > 0:
        timeLabel = Label(root, text=time_left)
        timeLabel.pack()
        time_left -= 1
        time.sleep(1)
        print (time_left)

Timer()

root.mainloop()

I only put the print statement there to know if it's working, which it is. In the console it's updating every second counting down from 15. But the tkinter window doesn't even open. It only opens when the While loop finishes and I just get a big list from 15 down to 1 instead of it opening in the beginning and updating every second. Why?

  • Have you tried [this](https://stackoverflow.com/questions/10596988/making-a-countdown-timer-with-python-and-tkinter) method for creating a timer that actually runs in parallel? – Random Davis Nov 17 '20 at 22:15
  • There are countless questions on this site related to timers and sleep and clocks and things not being updated. Have you done any research? – Bryan Oakley Nov 17 '20 at 23:31

1 Answers1

0

You call root.mainloop() after Timer(), meaning that Timer() has to finish before Tkinter can start. If you want the timer to run in parallel that's not going to work.

Random Davis
  • 6,662
  • 4
  • 14
  • 24