Home > Software engineering >  How to create a countdown timer which continues to count after pressing a button once, updating a te
How to create a countdown timer which continues to count after pressing a button once, updating a te

Time:11-22

I'm trying to create a tkinter window in which I have a timer counting down, and a button. Once I press the button, I'd like the timer to continuously count down. The text in the window should count down once every second as well, up until it hits 0:00.

So far, I've used this code. I've tried time.sleep() and window.after() to try and time it, but all my tkinter window is showing is either 2:00 before I press the button, and 0:00 one second later.


def countdown():
    total_seconds = 60
    total_minutes = 2

    while total_seconds != 0:
        if total_seconds == 60:
            total_seconds -= 1
            total_minutes -= 1
            time.sleep(1)
            canvas.itemconfig(timer_text, text=f"{total_minutes}:{total_seconds}")
        elif total_seconds == 1 and total_minutes != 0:
            total_seconds  = 59
            time.sleep(1)
            canvas.itemconfig(timer_text, text=f"{total_minutes}:00")
        elif total_seconds == 0 and total_minutes > 0:
            total_seconds = 59
            total_minutes -= 1
            time.sleep(1)
            canvas.itemconfig(timer_text, text=f"{total_minutes}:{total_seconds}")
        elif total_seconds < 10:
            total_seconds -= 1
            time.sleep(1)
            canvas.itemconfig(timer_text, text=f"{total_minutes}:0{total_seconds}")
        else:
            total_seconds -= 1
            time.sleep(1)
            canvas.itemconfig(timer_text, text=f"{total_minutes}:{total_seconds}")

window = Tk()
window.title("Title comes here")
window.config(padx=100, pady=50, bg=BLUE)



canvas = Canvas(width=400, height=450, bg=BLUE, highlightthickness=0)
timer_text = canvas.create_text(210, 100, text="2:00", fill="white", font=(FONT_NAME, 35, "bold"))
canvas.grid(column=1, row=1)


start_button = Button(text="Start", command=countdown)
start_button.grid(column=0, row=2)


CodePudding user response:

Here is a solution without the while loop and implemented after like suggested. Your conditions to update the time were already very good.

import tkinter as tk
import time


def countdown(total_seconds=60, total_minutes=2):
    if total_seconds == 60:
        total_seconds -= 1
        total_minutes -= 1
        timer_text.config(text=f"{total_minutes}:{total_seconds}")
        window.after(1000, countdown, total_seconds, total_minutes)        
    elif total_seconds == 1 and total_minutes != 0:
        total_seconds  = 59
        timer_text.config(text=f"{total_minutes}:00")
        window.after(1000, countdown, total_seconds, total_minutes)        
    elif total_seconds == 0 and total_minutes > 0:
        total_seconds = 59
        total_minutes -= 1
        timer_text.config(text=f"{total_minutes}:{total_seconds}")
        window.after(1000, countdown, total_seconds, total_minutes)        
    elif total_seconds < 11 and total_seconds > 0:
        total_seconds -= 1
        timer_text.config(text=f"{total_minutes}:0{total_seconds}")
        window.after(1000, countdown, total_seconds, total_minutes)   
    elif total_seconds == 0 and total_minutes == 0:
        pass
    else:
        total_seconds -= 1
        timer_text.config(text=f"{total_minutes}:{total_seconds}")
        window.after(1000, countdown, total_seconds, total_minutes)        


window = tk.Tk()
window.title("Title comes here")
window.config(padx=100, pady=50, bg='BLUE')

timer_text = tk.Label(window, text="TIMER", font=('Arial', 35, "bold"), bg='blue')
timer_text.grid(column=1, row=1)

start_button = tk.Button(text="Start", command=countdown)
start_button.grid(column=0, row=2)

window.mainloop()

CodePudding user response:

It is not recommended to use while loop in tkinter application, use .after() instead.

Below is the modified countdown() using .after():

def countdown(seconds=120):
    mins, secs = divmod(seconds, 60)
    canvas.itemconfig(timer_text, text=f"{mins}:{secs:02}")
    if seconds > 0:
        canvas.after(1000, countdown, seconds-1)
  • Related