Skip to content

Commit 97cbdfc

Browse files
committed
Add weather_notifier.py and README.md
1 parent 0fa47cd commit 97cbdfc

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

weather_notifier/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Weather Notifier
2+
3+
This script provides a simple GUI application to display weather information for a specified city and can notify the user at regular intervals.
4+
5+
## Prerequisites
6+
7+
- Python 3.x
8+
- `requests` library
9+
10+
## Installation
11+
12+
1. Clone the repository or download the script.
13+
2. Install the required libraries using pip:
14+
```sh
15+
pip install requests
16+
```
17+
18+
## Setup
19+
20+
1. Obtain an API key from [OpenWeatherMap](https://openweathermap.org/api).
21+
2. Set the API key as an environment variable:
22+
- On Windows:
23+
```sh
24+
setx OPENWEATHERMAP_API_KEY "your_api_key_here"
25+
```
26+
- On macOS/Linux:
27+
```sh
28+
export OPENWEATHERMAP_API_KEY="your_api_key_here"
29+
```
30+
31+
## Usage
32+
33+
1. Navigate to the directory containing the script.
34+
2. Run the script:
35+
```sh
36+
python weather_notifier.py
37+
```
38+
3. Enter the city name and the interval for notifications.
39+
4. Click "Get Weather" to display the current weather.
40+
5. Click "Start Notifier" to start receiving weather notifications at the specified interval.
41+
6. Click "Stop Notifier" to stop the notifications.
42+
43+
## Features
44+
45+
- Display current weather information for a specified city.
46+
- Notify the user at regular intervals with updated weather information.
47+
- Supports intervals in seconds, minutes, hours, and days.
48+
49+
## Acknowledgements
50+
51+
- [OpenWeatherMap](https://openweathermap.org/) for providing the weather API.

weather_notifier/weather_notifier.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import tkinter as tk
2+
from tkinter import messagebox
3+
import requests
4+
import time
5+
import threading
6+
import os
7+
8+
# Global variable to control the notifier state
9+
notifier_running = False
10+
11+
# Function to get weather data
12+
def get_weather(api_key, city):
13+
base_url = "http://api.openweathermap.org/data/2.5/weather?"
14+
complete_url = base_url + "appid=" + api_key + "&q=" + city
15+
response = requests.get(complete_url)
16+
return response.json()
17+
18+
# Function to display weather
19+
def display_weather():
20+
city = city_entry.get()
21+
api_key = os.getenv("OPENWEATHERMAP_API_KEY") # Get API key from environment variable
22+
if not api_key:
23+
messagebox.showerror("Error", "API key not found in environment variables")
24+
return
25+
26+
weather_data = get_weather(api_key, city)
27+
28+
if weather_data["cod"] != "404":
29+
main = weather_data["main"]
30+
weather_desc = weather_data["weather"][0]["description"]
31+
temp_celsius = main["temp"] - 273.15 # Convert from Kelvin to Celsius
32+
temp_fahrenheit = (temp_celsius * 9/5) + 32 # Convert from Celsius to Fahrenheit
33+
messagebox.showinfo("Weather Info", f"City: {city}\nTemperature: {temp_celsius:.2f}°C / {temp_fahrenheit:.2f}°F\nDescription: {weather_desc}")
34+
else:
35+
messagebox.showerror("Error", "City Not Found")
36+
37+
# Function to run the notifier at fixed intervals
38+
def run_notifier(interval):
39+
global notifier_running
40+
while notifier_running:
41+
display_weather()
42+
time.sleep(interval)
43+
44+
# Function to start the notifier in a separate thread
45+
def start_notifier():
46+
global notifier_running
47+
notifier_running = True
48+
interval = int(interval_entry.get())
49+
time_unit = time_unit_var.get()
50+
51+
if time_unit == "Minute(s)":
52+
interval *= 60
53+
elif time_unit == "Hour(s)":
54+
interval *= 3600
55+
elif time_unit == "Day(s)":
56+
interval *= 86400
57+
58+
threading.Thread(target=run_notifier, args=(interval,)).start()
59+
60+
# Function to stop the notifier
61+
def stop_notifier():
62+
global notifier_running
63+
notifier_running = False
64+
65+
# GUI setup
66+
root = tk.Tk()
67+
root.title("Weather Notifier")
68+
69+
tk.Label(root, text="City:").grid(row=0, column=0)
70+
city_entry = tk.Entry(root)
71+
city_entry.grid(row=0, column=1)
72+
73+
tk.Label(root, text="Interval:").grid(row=1, column=0)
74+
interval_entry = tk.Entry(root)
75+
interval_entry.grid(row=1, column=1)
76+
77+
time_unit_var = tk.StringVar(value="Second(s)")
78+
time_unit_menu = tk.OptionMenu(root, time_unit_var, "Second(s)", "Minute(s)", "Hour(s)", "Day(s)")
79+
time_unit_menu.grid(row=1, column=2)
80+
81+
tk.Button(root, text="Get Weather", command=display_weather).grid(row=2, column=0)
82+
tk.Button(root, text="Start Notifier", command=start_notifier).grid(row=2, column=1)
83+
tk.Button(root, text="Stop Notifier", command=stop_notifier).grid(row=2, column=2)
84+
85+
root.mainloop()

0 commit comments

Comments
 (0)