Nerdegutta's logo

nerdegutta.no

How to make a clock in Python and Tkinter

26.12.23

Programming

This little program will display a digital clock on the screen.

'''Import libraries'''
from tkinter import *
import time

'''Define a function'''
def tick():
'''Assign current time to time_string'''
time_string = time.strftime("%H:%M:%S")

'''Update the clock labels text'''
clock.config(text=time_string)

'''After 200ms run tick'''
clock.after(200, tick)

'''Make the window'''
window = Tk()

'''Make the Label clock, put in on window and give it som attributes'''
clock = Label(window, font=("serif", 50, "bold"), bg="white")

'''Place the clock label'''
clock.grid(row=0, column=1)

'''Run the tick function'''
tick()

'''Setting the window title'''
window.title("Clock")

'''Place the window inthe top left corner'''
window.geometry("+0+0")

'''Prevent users to resize the window'''
window.resizable(False, False)

'''Run main loop'''
window.mainloop()