This project is a simple graphical user interface (GUI) application that displays the current date and time using Python's tkinter
library. The time updates every second and this application has a clean and easy to view appearance.
- Developer: Shabir Mahfudz Prahono - @shabir-mp
- Application creation date: 16 June 2024
- Real-Time Clock: The application updates the time every second.
- Current Date Display: The application displays the current date.
- Neat appearance: Neat and tidy application appearance.
- Python 3.x
tkinter
library (usually included with Python)Pillow
library for handling images
- Clone the repository:
git clone https://github.com/your-username/your-repo-name.git
- Navigate to the project directory:
cd your-repo-name
- Install the required packages:
pip install Pillow
-
Place your icon image: Ensure you have an icon image named
icon.png
in the same directory as the script. This image will be used as the window icon. -
Run the script:
python your_script_name.py
Replace
your_script_name.py
with the name of your Python script file.
The main components of the script are:
-
Imports:
tkinter
for creating the GUI.datetime
for getting the current date and time.PIL
(Pillow) for handling images.
import tkinter as tk from datetime import datetime from PIL import Image, ImageTk
-
Update Time Function:
- This function gets the current date and time and updates the labels.
- It also schedules itself to run again after 1 second (1000 milliseconds).
def update_time(): current_time = datetime.now().strftime('%H:%M:%S') current_date = datetime.now().strftime('%Y-%m-%d') time_label.config(text=current_time) date_label.config(text=current_date) root.after(1000, update_time)
-
Creating the Main Window:
- Initializes the main window.
- Sets the window title, size, and icon.
root = tk.Tk() root.title("Date and Time") root.geometry("600x250") image = Image.open('icon.png') icon = ImageTk.PhotoImage(image) root.iconphoto(False, icon)
-
Labels for Time and Date:
- Creates and packs the labels for displaying the time and date.
label = tk.Label(root, bg='white') label.pack() time_label = tk.Label(label, font=('Kanit', 80), fg='black', bg='white') time_label.pack(pady=20) date_label = tk.Label(label, font=('Kanit', 24), fg='black', bg='white') date_label.pack(pady=0)
-
Initial Update and Main Loop:
- Calls the
update_time
function to start the update cycle. - Starts the main loop of the
tkinter
window.
root.configure(bg='white') update_time() root.mainloop()
- Calls the
- Change Font: You can change the font and size of the time and date labels by modifying the
font
parameter in thetk.Label
definitions. - Change Colors: You can change the foreground and background colors by modifying the
fg
andbg
parameters in thetk.Label
definitions. - Window Size: You can adjust the window size by modifying the
root.geometry("600x250")
line.
This project is licensed under the MIT License. See the LICENSE file for more details.