simple yet effective prayer times notification widget using Python. This widget will fetch prayer times for a specific city, display the next prayer time, and notify the user when it's time for the prayer. We will use the Tkinter library for the GUI, the requests library to fetch data from an API, and the plyer library for notifications.
## Prerequisites
Before we start, ensure you have the following libraries installed:
The Code
Let's dive into the code step-by-step.
1. Importing Required Libraries
We start by importing the necessary libraries. Tkinter is used for the GUI, requests for API calls, and datetime for handling dates and times. Plyer will be used for sending notifications.
2. Fetching Prayer Times
We define a function to fetch prayer times from the Aladhan API. This function sends a GET request to the API endpoint and returns the prayer times for the specified city.
3. Finding the Next Prayer
Next, we create a function to determine the next prayer time. This function compares the current time with the prayer times and finds the upcoming prayer.
4. Updating the Widget
The `update_widget` function is responsible for updating the GUI with the next prayer time and sending a notification when it's time for the prayer. It also schedules the next update to occur every minute.
5. Setting Up the GUI
We use Tkinter to set up the GUI. The GUI will display the next prayer time and the remaining time until the next prayer.
Running the Code
Save the complete code in a file, for example, `prayer_times_widget.py`, and run it using Python:
You will see a GUI window displaying the next prayer time and the remaining time until the next prayer. When it's time for the prayer, a notification will pop up.
## Prerequisites
Before we start, ensure you have the following libraries installed:
Code:
pip install tkinter requests plyer
The Code
Let's dive into the code step-by-step.
1. Importing Required Libraries
We start by importing the necessary libraries. Tkinter is used for the GUI, requests for API calls, and datetime for handling dates and times. Plyer will be used for sending notifications.
Code:
import tkinter as tk
import requests
import datetime
from plyer import notification
2. Fetching Prayer Times
We define a function to fetch prayer times from the Aladhan API. This function sends a GET request to the API endpoint and returns the prayer times for the specified city.
Code:
# Function to fetch prayer times
def get_prayer_times():
city = "Cairo"
country = "Egypt"
api_url = f"http://api.aladhan.com/v1/timingsByCity?city={city}&country={country}&method=5"
response = requests.get(api_url)
data = response.json()
return data['data']['timings']
3. Finding the Next Prayer
Next, we create a function to determine the next prayer time. This function compares the current time with the prayer times and finds the upcoming prayer.
Code:
# Function to find the next prayer
def get_next_prayer(prayer_times):
now = datetime.datetime.now()
today_str = now.strftime("%Y-%m-%d")
next_prayer = None
next_prayer_time = None
for prayer, time in prayer_times.items():
prayer_time_str = f"{today_str} {time}"
prayer_time = datetime.datetime.strptime(prayer_time_str, "%Y-%m-%d %H:%M")
if prayer_time > now:
next_prayer = prayer
next_prayer_time = prayer_time
break
if not next_prayer:
tomorrow_str = (now + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
next_prayer = "Fajr"
next_prayer_time_str = f"{tomorrow_str} {prayer_times['Fajr']}"
next_prayer_time = datetime.datetime.strptime(next_prayer_time_str, "%Y-%m-%d %H:%M")
return next_prayer, next_prayer_time
4. Updating the Widget
The `update_widget` function is responsible for updating the GUI with the next prayer time and sending a notification when it's time for the prayer. It also schedules the next update to occur every minute.
Code:
# Function to update the widget
def update_widget():
prayer_times = get_prayer_times()
next_prayer, next_prayer_time = get_next_prayer(prayer_times)
remaining_time = next_prayer_time - datetime.datetime.now()
hours, remainder = divmod(remaining_time.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
remaining_str = f"{hours:02}h {minutes:02}m {seconds:02}s"
# Update label text
label.config(text=f"Next Prayer: {next_prayer}\nTime: {next_prayer_time.strftime('%H:%M')}\nRemaining: {remaining_str}")
# Check if it's time for prayer
if remaining_time.total_seconds() < 60:
notification.notify(
title="Prayer Time",
message=f"It's time for {next_prayer} prayer",
timeout=10
)
# Schedule the next update
root.after(60000, update_widget) # Update every minute
5. Setting Up the GUI
We use Tkinter to set up the GUI. The GUI will display the next prayer time and the remaining time until the next prayer.
Code:
# Set up the GUI
root = tk.Tk()
root.title("Prayer Times Widget")
# Styling the window
root.configure(bg="#f0f0f0")
frame = tk.Frame(root, bg="#f0f0f0")
frame.pack(padx=20, pady=20, fill="both", expand=True)
# Display the next prayer information
label = tk.Label(frame, font=('Helvetica', 18), bg="#f0f0f0", fg="#333")
label.pack(pady=20)
# Start the widget update
update_widget()
root.mainloop()
Running the Code
Save the complete code in a file, for example, `prayer_times_widget.py`, and run it using Python:
Code:
python prayer_times_widget.py
You will see a GUI window displaying the next prayer time and the remaining time until the next prayer. When it's time for the prayer, a notification will pop up.