-1

I'm working on countdown timer with GUI and I'm trying to copy the GUI of Windows 10 coundown timer app. The GUI part is almost done and only the execution part remains:

def start_countdown():
    global hours, minutes, seconds, timer
    user_input = (int(hours)*60*60 + int(minutes)*60 + int(seconds))

    timerrr = int(user_input)


    while timerrr > 0:
        minutes, seconds = divmod(timerrr, 60)
        hours, minutes = divmod(minutes, 60)
        timer.config(text=str(hours).zfill(2) + ":" + str(minutes).zfill(2) + ":" + str(seconds).zfill(2))
        print(str(timer.cget("text")) + "\r", end=str(timer.cget("text")))
        time.sleep(1)
        timerrr -= 1
        timer.after(1, start_countdown)

    if timerrr == 0:
        print("\r""time's up!!!")
        showinfo("countdown clock", "Time's Up!!!")

This is the function that is supposed to perform the countdown step on the press of the button. The "timer" label doesn't display the updates after every second as I used the .after() but by printing the same, I can see the countdown is working in console/terminal.

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
  • Due to you didn't post a minimal example,you could refer to this question: [how-can-i-use-sleep-properly](https://stackoverflow.com/questions/42651018/how-can-i-use-sleep-properly) – jizhihaoSAMA Jun 03 '20 at 02:30
  • Does this answer your question? [How can I use "sleep" properly?](https://stackoverflow.com/questions/42651018/how-can-i-use-sleep-properly) – 10 Rep Jun 03 '20 at 02:59
  • Does this answer your question? [making-python-tkinter-label-widget-update](https://stackoverflow.com/questions/1918005) – stovfl Jun 03 '20 at 07:44

1 Answers1

0

You used after() in a wrong way and also you should not use while loop and sleep() as it may block the main thread.

Below is a modified start_countdown():

def start_countdown(timerrr=None):
    '''
    based on your code, 'hours', 'minutes', 'seconds' and 'timer'
    should be already created in the global namespace
    '''
    if timerrr is None:
        # initialize timerrr when start_countdown() is first called
        timerrr = int(hours)*60*60 + int(minutes)*60 + int(seconds)
    mins, secs = divmod(timerrr, 60)
    hrs, mins = divmod(mins, 60)
    timer['text'] = f'{hrs:02}:{mins:02}:{secs:02}'
    if timerrr > 0:
        timer.after(1000, start_countdown, timerrr-1)
    else:
        showinfo("countdown clock", "Time's up!!")
acw1668
  • 40,144
  • 5
  • 22
  • 34