How To Create A Digital Clock in Python
Hello coder, welcome to the codewithrandom blog. In this article, we will create a digital clock using Python with Complete Source Code. The digital clock is one of the most commonly used tools in daily life. Python is a powerful programming language and it is widely used in various fields such as data science, web development, and software engineering. In this article we will learn how to create a Digital clock using Tkinter in Python to create a digital clock. Let’s get started!
To Run the Digital Clock in Python , you can follow these steps:
The following steps outline how to create a Digital Clock using Python and Tkinter:
step 1: open any python code Editor
step 2: Importing the Required Modules.
import tkinter as tk from time import strftime
step 3: Copy the code for the Digital Clock in Python, which I provided Below in this article, and save it in a file named “main.py” (or any other name you prefer).
step 4: Run this python file main.py to start the Clock.
complete code👇👇👇
import tkinter as tk from time import strftime def light_theme(): frame = tk.Frame(root, bg="white") frame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.8) lbl_1 = tk.Label(frame, font=('calibri', 40, 'bold'), background='White', foreground='black') lbl_1.pack(anchor="s") def time(): string = strftime('%I:%M:%S %p') lbl_1.config(text=string) lbl_1.after(1000, time) time() def dark_theme(): frame = tk.Frame(root, bg="#22478a") frame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.8) lbl_2 = tk.Label(frame, font=('calibri', 40, 'bold'), background='#22478a', foreground='black') lbl_2.pack(anchor="s") def time(): string = strftime('%I:%M:%S %p') lbl_2.config(text=string) lbl_2.after(1000, time) time() root = tk.Tk() root.title("Digital-Clock") canvas = tk.Canvas(root, height=140, width=400) canvas.pack() frame = tk.Frame(root, bg='#22478a') frame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.8) lbl = tk.Label(frame, font=('calibri', 40, 'bold'), background='#22478a', foreground='black') lbl.pack(anchor="s") def time(): string = strftime('%I:%M:%S %p') lbl.config(text=string) lbl.after(1000, time) time() menubar = tk.Menu(root) theme_menu = tk.Menu(menubar, tearoff=0) theme_menu.add_command(label="Light", command=light_theme) theme_menu.add_command(label="Dark", command=dark_theme) menubar.add_cascade(label="Theme", menu=theme_menu) root.config(menu=menubar) root.mainloop()
OutPut👇👇
Conclusion
In this blog post, we have create a digital clock using Python and tkinter. Finally, we will run the Tkinter main loop to display the window and update the clock every second. Overall, creating a digital clock using Python and Tkinter is a great beginner project that can teach you valuable skills in GUI programming, timekeeping, and event handling. Hope you enjoyed building with us! Visit our homepage and you get lot’s of projects💝.
How to Create a GUI Calendar Using Python