Making a countdown timer with Python and Tkinter?

I want to set a label in Tkinter using my countdown timer function. Right now all it does is set the lable to “10” once 10 is reached and I don’t really understand why. Also, even if I have the timer print to a terminal instead the “Time’s up!” bit never prints.

import time
import tkinter as tk

class App():
    def __init__(self):
        self.root = tk.Tk()
        self.label = tk.Label(text="null")
        self.label.pack()
        self.countdown()
        self.root.mainloop()

    # Define a timer.
    def countdown(self):
        p = 10.00
        t = time.time()
        n = 0
        # Loop while the number of seconds is less than the integer defined in "p"
        while n - t < p: 
            n = time.time()
            if n == t + p:
                self.label.configure(text="Time's up!")
            else:
                self.label.configure(text=round(n - t))

app=App()

Best answer

Tkinter already has an infinite loop running (the event loop), and a way to schedule things to run after a period of time has elapsed (using after). You can take advantage of this by writing a function that calls itself once a second to update the display. You can use a class variable to keep track of the remaining time.

import Tkinter as tk

class ExampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.label = tk.Label(self, text="", width=10)
        self.label.pack()
        self.remaining = 0
        self.countdown(10)

    def countdown(self, remaining = None):
        if remaining is not None:
            self.remaining = remaining

        if self.remaining <= 0:
            self.label.configure(text="time's up!")
        else:
            self.label.configure(text="%d" % self.remaining)
            self.remaining = self.remaining - 1
            self.after(1000, self.countdown)

if __name__ == "__main__":
    app = ExampleApp()
    app.mainloop()