A Simple Python Script of a Digital Clock
This is a simple sample script in Python.
from tkinter
import *
from datetime
import datetime
import time
c = Canvas(width
= 700, height = 300, background = '#00ffff')
c.pack()
try:
while True:
now = datetime.now()
s =
'{0:0>2d}:{1:0>2d}:{2:0>2d}'.format(now.hour, now.minute, now.second)
c.create_rectangle(0, 0, 700, 300, outline = '#00ffff', fill = '#00ffff')
c.create_text(350,
150, text = s, font = ('', 100), fill = 'blue')
c.update()
time.sleep(0.1)
except:
pass
Run this script, then
the form of Figure 1 will be shown.
Figure
1
The following statement reads the present
time into variable now.
now = datetime.now()
A string s, which describes the present
time, is created.
s = '{0:0>2d}:{1:0>2d}:{2:0>2d}'.format(now.hour,
now.minute, now.second)
The content of the canvas is updated, and the execution of the script is waiting for
0.1sec.
c.update()
time.sleep(0.1)
The execution of the script ends, when the icon X at the upper right corner of the Form is clicked. To avoid the error message, the gtry ~ except ~h structure is employed.