9

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()
1

1 Answer 1

14

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()
Sign up to request clarification or add additional context in comments.

2 Comments

That explains a lot, thank you. I was wondering why it only updates once the last item is reached even after I rewrote it a few different ways.
@RyanHasse: the display only updates when Tkinter can respond to events that tell it to update the display. These events only get processed by the event loop. If you have your own loop, it will starve the event loop, preventing redraw requests from happening.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.