forked from gmonu/weatherforecast-application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.py
64 lines (42 loc) · 1.83 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import tkinter as tk
from tkinter import font
import requests
hi = 500
wi = 600
#2c483bb058937a3ff2910c1895f1357e
#api.openweathermap.org/data/2.5/forecast?q={city name},{country code}
def test_function(entry):
print("this is entry: ", entry)
def format_response(weather):
try:
name = weather['name']
desc = weather['weather'][0]['description']
temp = weather['main']['temp']
final_str = 'City: %s \nConditions: %s \nTemperature (°F): %s' % (name, desc, temp)
except:
final_str = 'problem retrieving the information'
return final_str
def get_weather(city):
weather_key = '2c483bb058937a3ff2910c1895f1357e'
url = 'https://api.openweathermap.org/data/2.5/weather'
params = {'APPID': weather_key, 'q':city, 'units': 'imperial'}
response = requests.get(url, params=params)
weather = response.json()
label['text'] = format_response(weather)
root = tk.Tk()
canvas = tk.Canvas(root, height=hi, width=wi )
canvas.pack()
background_image = tk.PhotoImage(file='landscape.png')
backgorund_label = tk.Label(root, image=background_image)
backgorund_label.place(relwidth=1, relheight=1)
frame = tk.Frame(root, bg='#4295f5', bd=5)
frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')
entry = tk.Entry(frame, font=('courier', 14))
entry.place(relwidth=0.65, relheight=1)
button = tk.Button( frame, text="Get weather", font=('courier', 12), command=lambda: get_weather(entry.get()))
button.place(relx=0.7, relwidth=0.3, relheight=1)
lower_frame = tk.Frame(root, bg='#4295f5', bd=5 )
lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')
label = tk.Label(lower_frame, font=('courier', 17), anchor='nw', justify='left', bd=4)
label.place(relwidth=1, relheight=1)
root.mainloop()