TECH PIXEL

"Tech Pixel": Clearly mentions the site name. "Your go-to source for the latest tech news, reviews, and tutorials": Summarizes what the site offers. "Stay updated with the newest trends in technology": Highlights the value of staying current with tech trends. "Enhance your skills with our expert guides": Emphasizes the practical benefits and expertise available on the site.

Digital Clock using python

Digital Clock :

using python:

code:

import tkinter as tk
import time


# Function to update the clock every second
def time_update():
    current_time = time.strftime('%H:%M:%S %p')
    current_date = time.strftime('%d:%m:%Y')

    hours, minutes, seconds, period = current_time.split(':')[:2] + current_time.split(':')[2].split()
    day, month, year = current_date.split(':')

    hours_label.config(text=hours)
    minutes_label.config(text=minutes)
    seconds_label.config(text=seconds)
    period_label.config(text=period)

    day_label.config(text=day)
    month_label.config(text=month)
    year_label.config(text=year)

    clock.after(1000, time_update)


# Create the main window
clock = tk.Tk()
clock.title(' ******* Digital Clock ******* ')
clock.geometry('1000x300')
clock.configure(bg='black')

# Create frames for each part of the time
frame_hours = tk.Frame(clock, bg='black', width=100, height=100)
frame_minutes = tk.Frame(clock, bg='black', width=100, height=100)
frame_seconds = tk.Frame(clock, bg='black', width=100, height=100)
frame_period = tk.Frame(clock, bg='black', width=100, height=100)

frame_day = tk.Frame(clock, bg='black', width=200, height=100)
frame_month = tk.Frame(clock, bg='black', width=200, height=100)
frame_year = tk.Frame(clock, bg='black', width=200, height=100)

frame_hours.pack(side=tk.LEFT, expand=True, padx=10, pady=10)
frame_minutes.pack(side=tk.LEFT, expand=True, padx=10, pady=10)
frame_seconds.pack(side=tk.LEFT, expand=True, padx=10, pady=10)
frame_period.pack(side=tk.LEFT, expand=True, padx=10, pady=10)

frame_day.pack(side=tk.LEFT, expand=True, padx=10, pady=10)
frame_month.pack(side=tk.LEFT, expand=True, padx=10, pady=10)
frame_year.pack(side=tk.LEFT, expand=True, padx=10, pady=10)

# Create labels to display hours, minutes, seconds, period, day, month, and year
hours_label = tk.Label(frame_hours, font=('Arial', 50), bg='green', fg='white')
minutes_label = tk.Label(frame_minutes, font=('Arial', 50), bg='green', fg='white')
seconds_label = tk.Label(frame_seconds, font=('Arial', 50), bg='green', fg='white')
period_label = tk.Label(frame_period, font=('Arial', 50), bg='green', fg='white')

day_label = tk.Label(frame_day, font=('Arial', 50), bg='purple', fg='white')
month_label = tk.Label(frame_month, font=('Arial', 50), bg='purple', fg='white')
year_label = tk.Label(frame_year, font=('Arial', 50), bg='purple', fg='white')

hours_label.pack(padx=10, pady=10)
minutes_label.pack(padx=10, pady=10)
seconds_label.pack(padx=10, pady=10)
period_label.pack(padx=10, pady=10)

day_label.pack(padx=10, pady=10)
month_label.pack(padx=10, pady=10)
year_label.pack(padx=10, pady=10)

# Create labels to indicate what each frame represents
hours_text_label = tk.Label(frame_hours, text='Hours', font=('Arial', 20), bg='green', fg='white', pady=10)
minutes_text_label = tk.Label(frame_minutes, text='Minutes', font=('Arial', 20), bg='green', fg='white', pady=10)
seconds_text_label = tk.Label(frame_seconds, text='Seconds', font=('Arial', 20), bg='green', fg='white', pady=10)
period_text_label = tk.Label(frame_period, text='AM/PM', font=('Arial', 20), bg='green', fg='white', pady=10)

day_text_label = tk.Label(frame_day, text='Day', font=('Arial', 20), bg='red', fg='white', pady=10)
month_text_label = tk.Label(frame_month, text='Month', font=('Arial', 20), bg='red', fg='white', pady=10)
year_text_label = tk.Label(frame_year, text='Year', font=('Arial', 20), bg='red', fg='white', pady=10)

hours_text_label.pack()
minutes_text_label.pack()
seconds_text_label.pack()
period_text_label.pack()

day_text_label.pack()
month_text_label.pack()
year_text_label.pack()

# Start the clock
time_update()

# Run the application
clock.mainloop()
  1. Run the program in PyCharm.

This code creates a simple digital clock that updates every second. Here's a breakdown of the key parts of the code:

  • The time_update function gets the current time using time.strftime and updates the text of clock_label with the current time. The clock_label.after(1000, time_update) line schedules the time_update function to be called again after 1000 milliseconds (1 second), ensuring the clock updates every second.

  • The clock object is the main window, and clock_label is the label that displays the time.

  • The clock.title('Digital Clock') and clock.geometry('500x200') lines set the title and size of the main window, respectively.

  • The clock.configure(bg='black') and clock_label.config(bg='black', fg='white') lines set the background color of the window and the label, and the text color of the label, respectively.

This should display a digital clock in a window, updating every second.

- Frames for the day, month, and year are added with specific widths and heights.

- Labels for each of these new frames display the current day, month, and year.

- The `time_update` function is updated to fetch and split the current date into day, month, and year, and update the corresponding labels.

- Padding is applied to the labels and frames to create space and better layout.

GitHub:

https://github.com/AroojAmina/python-projects/commit/6835ae57bf4aeb8d17b25925d4c7d4da60cf1be9


Post a Comment

0 Comments