-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherApp.py
72 lines (51 loc) · 2.04 KB
/
weatherApp.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
import tkinter as tk
from tkinter import font
import requests
from PIL import Image, ImageTk
HEIGTH = 600
WIDTH = 700
def format_response(weather):
try:
name = weather['name']
clouds = weather['weather'][0]['description']
temp = weather['main']['temp']
final_str = 'City: %s \nConditions: %s \nTemperature[C]: %s' %(name,clouds,temp)
except:
final_str = 'There was a problem retrieving that information'
return final_str
def get_weather(city):
weather_key = '4c95087b61beee413c02eb800dd7aca2'
url = 'https://api.openweathermap.org/data/2.5/weather'
params = {'APPID': weather_key, 'q': city, 'units':'metric'}
response = requests.get(url,params=params)
weather = response.json()
label['text'] = format_response(weather)
icon_name = weather['weather'][0]['icon']
open_image(icon_name)
def open_image(icon):
size = int(lower_frame.winfo_height()*0.25)
img = ImageTk.PhotoImage(Image.open('./img/'+icon+'.png').resize((size, size)))
weather_icon.delete("all")
weather_icon.create_image(0,0, anchor='nw', image=img)
weather_icon.image = img
root = tk.Tk()
canvas = tk.Canvas(root,height=HEIGTH,width=WIDTH)
canvas.pack()
background_image = tk.PhotoImage(file='landscape.png')
background_label = tk.Label(root,image=background_image)
background_label.place(relwidth=1,relheight=1)
# upper frame
frame = tk.Frame(root,bg='#80c1ff',bd=5)
frame.place(relx= 0.5, rely=0.1, relheight=0.1,relwidth=0.75,anchor='n')
entry = tk.Entry(frame,font=40)
entry.place(relwidth=0.65,relheight=1)
button = tk.Button(frame,text='Get weather',font=40,command=lambda: get_weather(entry.get()))
button.place(relx=0.7,relwidth=0.3,relheight=1)
# lower frame
lower_frame = tk.Frame(root,bg='#80c1ff',bd=10)
lower_frame.place(relx=0.5, rely=0.25, relheight=0.6,relwidth=0.75,anchor='n')
label = tk.Label(lower_frame,font=('courier',18),anchor='nw',justify='left')
label.place(relwidth=1,relheight=1)
weather_icon = tk.Canvas(label, bg='white', bd=0, highlightthickness=0)
weather_icon.place(relx=.75, rely=0, relwidth=1, relheight=0.5)
root.mainloop()