import tkinter as tk
from tkinter import font
import requests
HEIGHT = 556
WIDTH = 724

def test_function(entry):
    print("This is the 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 (ºC): %s' % (name,desc,temp)
    except:
        final_str = 'There was a problem retrieving that information'
    return final_str
def get_weather(city):
    weather_key= '0a2db83a59da8e61fc1957a66c765f80'
    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)

root = tk.Tk()
#api.openweathermap.org/data/2.5/forecast?q={city name},{country code}

canvas = tk.Canvas(root, height=HEIGHT,width=WIDTH)
canvas.pack()

background_image = tk.PhotoImage(file='Garden.png')
background_label = tk.Label(root,image=background_image)
background_label.place(relwidth=1,relheight=1)

frame = tk.Frame(root, bg='#80c1ff',bd='5')
frame.place(relx=0.5,rely=0.1,relwidth=0.75,relheight=0.1,anchor= 'n')

entry = tk.Entry(frame, font=('Courier',12))
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='#80c1ff',bd=10)
lower_frame.place(relx=0.5,rely=0.25, relwidth=0.75,relheight=0.6,anchor='n')

label = tk.Label(lower_frame,bg='white', font=('Courier',20),anchor='nw',justify='left',bd=4)
label.place(relwidth=1,relheight=1) 

root.mainloop() 