-
Notifications
You must be signed in to change notification settings - Fork 159
/
WeatherApp_withicons.py
76 lines (54 loc) · 2.32 KB
/
WeatherApp_withicons.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
75
76
import tkinter as tk
import requests
from PIL import Image, ImageTk
app = tk.Tk()
HEIGHT = 500
WIDTH = 600
def format_response(weather_json):
try:
city = weather_json['name']
conditions = weather_json['weather'][0]['description']
temp = weather_json['main']['temp']
final_str = 'City: %s \nConditions: %s \nTemperature (°F): %s' % (city, conditions, temp)
except:
final_str = 'There was a problem retrieving that information'
#final_str = 'hello'
return final_str
def get_weather(city):
weather_key = 'edffd1bf975a74d5d10e58c5ac8be2d3'
url = 'https://api.openweathermap.org/data/2.5/weather'
params = {'APPID': 'edffd1bf975a74d5d10e58c5ac8be2d3', 'q': city, 'units':'imperial'}
response = requests.get(url, params=params)
print(response.json())
weather_json = response.json()
results['text'] = format_response(response.json())
icon_name = weather_json['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
C = tk.Canvas(app, height=HEIGHT, width=WIDTH)
background_image= tk.PhotoImage(file='./landscape.png')
background_label = tk.Label(app, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
frame = tk.Frame(app, bg='#42c2f4', bd=5)
frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')
#frame_window = C.create_window(100, 40, window=frame)
textbox = tk.Entry(frame, font=40)
textbox.place(relwidth=0.65, relheight=1)
submit = tk.Button(frame, text='Get Weather', font=40, command=lambda: get_weather(textbox.get()))
#submit.config(font=)
submit.place(relx=0.7, relheight=1, relwidth=0.3)
lower_frame = tk.Frame(app, bg='#42c2f4', bd=10)
lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')
bg_color = 'white'
results = tk.Label(lower_frame, anchor='nw', justify='left', bd=4)
results.config(font=40, bg=bg_color)
results.place(relwidth=1, relheight=1)
weather_icon = tk.Canvas(results, bg=bg_color, bd=0, highlightthickness=0)
weather_icon.place(relx=.75, rely=0, relwidth=1, relheight=0.5)
app.mainloop()