This short Python code gets the local time from the PC as a formatted string using time.strftime('%H:%M:%S'). The time string is displayed in a label using a larger font. A recursive function checks the time five times per second, and updates the time string, if it has changed. Five times per second may sound like overkill, but keeps the display from acting spasmodic.

# use Tkinter to show a digital clock
# tested with Python24    vegaseat    10sep2006

from Tkinter import *
import time

root = Tk()
time1 = ''
clock = Label(root, font=('times', 20, 'bold'), bg='green')
clock.pack(fill=BOTH, expand=1)

def tick():
    global time1
    # get the current local time from the PC
    time2 = time.strftime('%H:%M:%S')
    # if time string has changed, update it
    if time2 != time1:
        time1 = time2
        clock.config(text=time2)
    # calls itself every 200 milliseconds
    # to update the time display as needed
    # could use >200 ms, but display gets jerky
    clock.after(200, tick)

tick()
root.mainloop(  )

Why compare time2 and time1 when you can just directly call clock.after(200,tick)? It will update time anyways. I tried it and it worked.

The smallest time displayed is seconds. The function is called every 2/10 of a second, so the clock is not updated if the time to the second has not changed.

Okay.I am new,and I don't understand things fast, so please bear with me. What I meant to say was, why is time comparision b/w time1 and time2 required? If you just call the function every 2/10 of a second, time2 = time.strftime("%H:%M:%S:") time2, is itself getting updated every 2/10 of a second and clock.config(text=time2) will display that. There is no use of time1 here.
This is what I mean:

from Tkinter import *
import time
root = Tk()

clock = Label(root, font=('times', 20, 'bold'), bg='green')
clock.pack(fill=BOTH, expand=1)
def tick():
# get the current local time from the PC
    time2 = time.strftime('%H:%M:%S')
# if time string has changed, update it
    clock.config(text=time2)
# calls itself every 200 milliseconds
# to update the time display as needed
# could use >200 ms, but display gets jerky
    clock.after(200, tick)
tick()
root.mainloop( )

It will work the same way the given code is working.

Edited 2 Years Ago by Aggressive.Panda: not time, but time1

That is because unnecessary update of the screen weakens the quality of the display. Maybe not, or only with slower computer, but it is good practice to avoid unnecessary refresh. Especially when you do more action on screen than just changing number.

Here is a perhaps more readable version

try: from Tkinter import *
except ImportError: from tkinter import *
import time

root = Tk()
clock = Label(root, font=('times', 20, 'bold'), bg='green')
clock.pack(fill=BOTH, expand=1)

def tick():
    s = time.strftime('%H:%M:%S')
    if s != clock["text"]:
        clock["text"] = s
    clock.after(200, tick)

tick()
root.mainloop()

When Python3 came around I updated much of my Tkinter code samples. So I looked back and this is what I did with the tk_clock ...

''' tk_clock101.py
use Tkinter to show a digital clock
tested with Python27 and Python33
'''

import time
try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

def tick(time1=''):
    # get the current local time from the PC
    time2 = time.strftime('%H:%M:%S')
    # if time string has changed, update it
    if time2 != time1:
        time1 = time2
        clock.config(text=time2)
    # calls itself every 200 milliseconds
    # to update the time display as needed
    clock.after(200, tick)

root = tk.Tk()
clock = tk.Label(root, font=('times', 20, 'bold'), bg='green')
clock.pack(fill='both', expand=1)
tick()
root.mainloop()

Almost 8 years have gone by since the original code. The speed of graphics chips has much improved, but it is still prudent not to stress the display unnecessarily, as pyTony has pointed out. The somewhat embarrasing global is gone too.

There is always a better mouse trap, so please improve the code as needed.

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.