forked from turtlecode/Make-Weather-Forecast-App-Python-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (60 loc) · 2.45 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
import tkinter as tk
import requests
import time
import screeninfo
def getWeather(root):
city = cityEntry.get()
unit = "standard"
symbol = "K"
if unitsVariable.get() == "Celsius":
unit = "metric"
symbol = "°C"
elif unitsVariable.get() == "Fahrenheit":
unit = "imperial"
symbol = "°F"
else:
unit = "standard"
symbol = "K"
api = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=f01e80368f05c66b03425d3f08ab1a1c" + "&units=" + unit
json_data = requests.get(api).json()
if json_data["cod"] == "404":
cityEntry.delete(0, tk.END)
cityEntry.insert(0, "Invalid city")
return
condition = json_data['weather'][0]['main']
temp = int(json_data['main']['temp'])
min_temp = int(json_data['main']['temp_min'])
max_temp = int(json_data['main']['temp_max'])
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'] - 21600))
sunset = time.strftime('%I:%M:%S', time.gmtime(json_data['sys']['sunset'] - 21600))
final_info = condition + "\n" + str(temp) + symbol
final_data = "\n" + "Min Temp: " + str(min_temp) + symbol + "\n" + "Max Temp: " + str(
max_temp) + symbol + "\n" + "Pressure: " + str(pressure) + "\n" + "Humidity: " + str(
humidity) + "\n" + "Wind Speed: " + str(wind) + "\n" + "Sunrise: " + sunrise + "\n" + "Sunset: " + sunset
label1.config(text=final_info)
label2.config(text=final_data)
screenWidth = screeninfo.get_monitors()[0].width
screenHeight = screeninfo.get_monitors()[0].height
root = tk.Tk()
root.geometry(f"600x500+{int(screenWidth/2-600/2)}+{int(screenHeight/2-500/2)}")
root.title("Weather App")
root.resizable(False, False)
cityLabelFrame = tk.LabelFrame(root, text="Enter a city")
cityLabelFrame.pack(padx=20)
cityEntry = tk.Entry(cityLabelFrame, justify='center', width=20, font=("poppins", 35, "bold"))
cityEntry.pack(padx=20, pady=20)
cityEntry.focus()
cityEntry.bind('<Return>', getWeather)
unitsVariable = tk.StringVar()
units = ["Celsius", "Fahrenheit", "Kelvin"]
optionMenu = tk.OptionMenu(root, unitsVariable, *units)
optionMenu.pack(fill=tk.X, padx=20)
unitsVariable.set("Celsius")
label1 = tk.Label(root, font=("poppins", 35, "bold"))
label1.pack()
label2 = tk.Label(root, font=("poppins", 15, "bold"))
label2.pack()
root.mainloop()