Simple Analog Clock using Tkinter in Python
In this article, we will create a simple analog clock using Tkinter in Python.
First, we need to import the required libraries:
import tkinter as tk
from tkinter import Canvas
from tkinter.ttk import Label
from time import strftime
Next, we will create a window and canvas for our clock:
root = tk.Tk()
root.title("Analog Clock")
canvas = Canvas(root, width=400, height=400)
canvas.pack()
Now, we will create a function to draw the clock face:
def create_clock():
canvas.create_oval(50, 50, 350, 350, outline='black', width=2)
canvas.create_text(200, 100, text='12', font=('Helvetica', 12))
canvas.create_text(350, 200, text='3', font=('Helvetica', 12))
canvas.create_text(200, 350, text='6', font=('Helvetica', 12))
canvas.create_text(50, 200, text='9', font=('Helvetica', 12))
Next, we will create a function to update the clock every second:
def update_clock():
canvas.delete("time")
time = strftime('%H:%M:%S %p')
canvas.create_text(200, 200, text=time, font=('Helvetica', 12), tags="time")
root.after(1000, update_clock)
Finally, we will call the create_clock and update_clock functions to start our clock:
create_clock()
update_clock()
root.mainloop()
And that’s it! You have now created a simple analog clock using Tkinter in Python.