-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
64 lines (51 loc) · 2.57 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Import necessary libraries
import tkinter as tk # Tkinter library for GUI
import requests # Requests library for making HTTP requests
import time # Time library for manipulating time data
# Function to retrieve weather information
def getWeather(canvas):
try:
# Get the city name from the text entry field
city = textField.get()
# API endpoint for weather data
api = "https://api.openweathermap.org/data/2.5/weather?q="+city+"&appid=9036b0259df683d94625d841023d526f"
# Send HTTP GET request to the API and retrieve JSON response
json_data = requests.get(api).json()
condition = json_data['weather'][0]['main']
temp = int(json_data['main']['temp'] - 273.15) # change from Kelvin to Celsius
min_temp = int(json_data['main']['temp_min'] - 273.15) # change from Kelvin to Celsius
max_temp = int(json_data['main']['temp_max'] - 273.15) # change from Kelvin to Celsius
pressure = json_data['main']['pressure']
humidity = json_data['main']['humidity']
wind = json_data['wind']['speed']
sunrise = time.strftime('%I:%M:%S', time.gmtime(json_data['sys']['sunrise']))
sunset = time.strftime('%I:%M:%S', time.gmtime(json_data['sys']['sunset']))
# Prepare the weather information to display
final_info = condition + "\n" + str(temp) + "°C"
final_data = "\n"+ "Min Temp: " + str(min_temp) + "°C" + "\n" + "Max Temp: " + str(max_temp) + "°C" +"\n" + "Pressure: " + str(pressure) + "\n" +"Humidity: " + str(humidity) + "\n" +"Wind Speed: " + str(wind) + "\n" + "Sunrise: " + sunrise + "\n" + "Sunset: " + sunset
# Update the label widgets with the weather information
label1.config(text = final_info)
label2.config(text = final_data)
# Display an error message if an exception occurs during weather retrieval
except Exception as e:
label1.config(text = "OOPS!")
label2.config(text = "No city found")
# Create the main application window
canvas = tk.Tk()
canvas.geometry("600x500")
canvas.title("Cloud Commando Weather App")
# Font styles for the text
f = ("poppins", 15, "bold")
t = ("poppins", 35, "bold")
# Create text entry field for city input
textField = tk.Entry(canvas, justify='center', width = 20, font = t)
textField.pack(pady = 30)
textField.focus()
textField.bind('<Return>', getWeather)
# Create labels for displaying weather information
label1 = tk.Label(canvas, font=t)
label1.pack()
label2 = tk.Label(canvas, font=f)
label2.pack()
# Start the Tkinter event loop
canvas.mainloop()