To make a clock in just 15 lines using tkinter in Python, follow the steps below:
-
Import the required modules:
from tkinter import * from time import strftime
-
Create a tkinter window:
root = Tk() root.title("Digital Clock")
-
Create a function to update the time:
def time(): string = strftime("%H:%M:%S %p") label.config(text=string) label.after(1000, time)
-
Create a label to display the time:
label = Label(root, font=("calibri", 40), background="black", foreground="white") label.pack(anchor='center')
-
Call the time function to start updating the clock:
time()
-
Set the window size and run the tkinter main loop:
root.geometry("400x100") root.mainloop()
-
This simple program will create a digital clock that updates every second displaying the current time in the format HH:MM:SS AM/PM.
-
Save the code in a Python file, for example,
clock.py
. -
Run the Python script in your terminal or IDE to see the clock in action.
-
You can customize the clock further by changing the font, colors, size, or format of the displayed time.
-
You can also add additional features like alarm functionality or countdown timers to the clock.
-
Experiment with different tkinter widgets and layouts to create a more interactive clock display.
-
Learn more about tkinter GUI programming to enhance your clock and create more complex applications.
-
Share your digital clock creation with others and showcase your programming skills.
- Enjoy creating your own clock using tkinter in Python and have fun exploring the world of programming!
Your videos are so informative and entertaining, I just subscribed!