How to create a timer using tkinter ?

How to create a timer using tkinter?

Asked on November 22, 2018 in tkinter.
Add Comment

  • 3 Answer(s)

    Here is best solution:

        The Tkinter root windows have a method called after which can be used to schedule a function to be called after a given period of time. If that function itself calls after you’ve set up an automatically recurring event.

    The working Example:

    # for python 3.x use 'tkinter' rather than 'Tkinter'
    import Tkinter as tk
    import time
    class App():
        def __init__(self):
             self.root = tk.Tk()
             self.label = tk.Label(text="")
        def update_clock(self):
            now = time.strftime("%H:%M:%S")
            self.root.after(1000, self.update_clock)

    In my  mind that after doesn’t guarantee the function will run exactly on time. It only schedules the job to be run after a given amount of time. It the app is busy there may be a delay before it is called since Tkinter is single-threaded. The delay is typically measured in microseconds.

    Answered on November 22, 2018.
    Add Comment

    The best answer:

    The Python3 clock example using the frame.after() rather than the top level application. Also shows updating the label with a StringVar()

    #!/usr/bin/env python3
    # Display UTC.
    # started with
    import tkinter as tk
    import time
        def current_iso8601():
            """Get current date and time in ISO8601"""
            return time.strftime("%Y%m%dT%H%M%SZ", time.gmtime())
    class Application(tk.Frame):
        def __init__(self, master=None):
            tk.Frame.__init__(self, master)
        def createWidgets(self):
    = tk.StringVar()
             self.time = tk.Label(self, font=('Helvetica', 24))
             self.time["textvariable"] =
           self.QUIT = tk.Button(self, text="QUIT", fg="red", command=root.destroy)
            # initial time display
        def onUpdate(self):
            # update displayed time
            # schedule timer to call myself after 1 second
            self.after(1000, self.onUpdate)
    root = tk.Tk()
    app = Application(master=root)
    Answered on November 22, 2018.
    Add Comment

    Here is the another solution:

    from tkinter import *
    import time
    def clock():
        if t!='':
            label1.config(text=t,font='times 25')
    Answered on November 22, 2018.
    Add Comment

  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.