-
Launch the Python command-line interpreter.
-
Type the following commands to initialize the GUI:
from tkinter import *
root = Tk()
clock = Label(root)
clock.pack()
-
Define a variable to store the previously accessed time. Note that the equal sign is followed by two single quote marks.
prevtime = ''
You'll compare this variable to the current time to see if the clock needs to be updated.
-
Declare a function to update the clock:
def updateclock():
-
Entab the following three commands to make them part of the function:
global prevtime
newtime = time.strftime('%I:%M:%S %p')
if prevtime != newtime:
The "strftime" function returns a readable string containing the time. "Prevtime" is declared as global so that its value will persist across multiple calls to "updateclock."
-
Double-entab these next two commands to make them part of the "if" statement:
prevtime = newtime
clock.configure(text=newtime)
These commands update the clock and the previously accessed time variable if necessary.
-
Single-entab the following command to make the function recur periodically:
clock.after(500, updateclock)
-
Insert a blank line to close the function. The regular interpreter prompt (">>>") returns.
-
Start the clock by calling the "updateclock" function:
updateclock()
Your clock is now functioning in its window.