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 } \n Temperature: { temp_celsius :.2f} °C / { temp_fahrenheit :.2f} °F\n Description: { 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