-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
79 lines (58 loc) · 2.53 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
75
76
77
78
79
import tkinter as tk
import requests
from PIL import Image, ImageTk
root = tk.Tk()
root.title("Weather App")
root.geometry("600x500")
# Key: 2c481575c96a35678b7b4ec3d1f441d6
# API url: api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}
def format_response(weather):
try:
city = (weather['name'])
condition = (weather['weather'][0]['description'])
temp = (weather['main']['temp'])
final_str = 'City:%s\nCondition:%s\nTemperature:%s' % (city, condition, temp)
except:
final_str = 'There was problem retrieving information'
return final_str
def get_weather(city):
weather_key = '2c481575c96a35678b7b4ec3d1f441d6'
url = 'https://api.openweathermap.org/data/2.5/weather'
params = {'APPID': weather_key, 'q': city, 'units': 'imperial'}
response = requests.get(url, params)
# print(response.json())
weather = response.json()
# print(weather['name'])
# print(weather['weather'][0]['description'])
# print(weather['main']['temp'])
result['text'] = format_response(weather)
icon_name = weather['weather'][0]['icon']
open_image(icon_name)
def open_image(icon):
size = int(frame_two.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
img = Image.open('./bg.png')
img = img.resize((600, 500), Image.ANTIALIAS)
img_photo = ImageTk.PhotoImage(img)
bg_lbl = tk.Label(root, image=img_photo)
bg_lbl.place(x=0, y=0, width=600, height=500)
heading_title = tk.Label(bg_lbl, text='Know Temperature of 10,000 Cities!', fg='black', bg='sky blue',
font=('times new roman', 16, 'bold'))
heading_title.place(x=80, y=18)
frame_one = tk.Frame(bg_lbl, bg="#42c2f4", bd=5)
frame_one.place(x=80, y=60, width=450, height=50)
txt_box = tk.Entry(frame_one, font=('times new roman', 25), width=17)
txt_box.grid(row=0, column=0, sticky="w")
btn = tk.Button(frame_one, text="Get Weather", fg='green', font=('times new roman', 16, 'bold'),
command=lambda: get_weather(txt_box.get()))
btn.grid(row=0, column=1, padx=10)
frame_two = tk.Frame(bg_lbl, bg="#42c2f4", bd=5)
frame_two.place(x=80, y=130, width=450, height=300)
result = tk.Label(frame_two, font=40, bg='white', justify='left', anchor='nw')
result.place(relwidth=1, relheight=1)
weather_icon = tk.Canvas(result, bg='white', bd=0)
weather_icon.place(relx=.75, rely=0, relwidth=1, relheight=0.5)
root.mainloop()