-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.py
43 lines (34 loc) · 1.47 KB
/
weather.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
import tkinter as tk
import requests
import time
def getWeather(canvas):
city = textfield.get()
api = "http://api.openweathermap.org/data/2.5/weather?q="+ city +"&appid=74943fdae2977e9fdca8f24899f6bb00"
json_data = requests.get(api).json()
condition = json_data['weather'][0]['main']
temp = int(json_data['main']['temp'] - 273.15)
min_temp = int(json_data['main']['temp_min'] - 273.15)
max_temp = int(json_data['main']['temp_max'] - 273.15)
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']))
final_info = condition + "\n" + str(temp) + "C\n"
final_data = "Max Temp: " + str(max_temp) + "\n" + "Min Temp: " + str(min_temp) + "\n" + "Pressure: " + str(pressure) + "\n" + "Wind: " + str(wind) + "\n" + "Sunrise: " + str(sunrise) + "\n" + "Sunset: " + str(sunset)
label1.config(text = final_info)
label2.config(text= final_data)
canvas = tk.Tk()
canvas.geometry("600x500")
canvas.title("Weather App")
f = ("poppins", 15, "bold")
t = ("poppins", 35, "bold")
textfield = tk.Entry(canvas, justify = "center", font = t)
textfield.pack(pady = 20)
textfield.focus()
textfield.bind('<Return>', getWeather)
label1 = tk.Label(canvas, font = t)
label1.pack()
label2 = tk.Label(canvas, font= f)
label2.pack()
canvas.mainloop()