0

Unable to fetch an icon from the URL


    from tkinter import *
    from PIL import ImageTk, Image
    import requests
    from io import BytesIO
    root = Tk()
    root.geometry("250x400")
    root.title("Weather Report")
    root.configure(bg="#50514F")
    
    
    def weather(root):
        try:
            cityName = inputCity.get()
#getting info from the API
            jsonData = requests.get(
                'http://api.openweathermap.org/data/2.5/weather?q=' + cityName + ` here`'&appid=ddb74ff40d******81caef7e945640e02').json()
            condition = jsonData['weather'][0]['main']
            temp = int(jsonData['main']['temp']) - 273.15
            desc = jsonData['weather'][0]['description']
            icon = jsonData['weather'][0]['icon']
            temp_min = int(jsonData['main']['temp_min']) - 273.15
            temp_max = int(jsonData['main']['temp_max']) - 273.15
            pressure = jsonData['main']['pressure']
    
            setDesc = condition + str(int(temp)) + "°C" + "\n" + desc + "\n"
            setInfo = str(int(temp_min)) + "°C" + " Min" + "\n" + str(int(temp_max)) + "°C" + " Max" + "\n" str(pressure) + "Pa"
            icon_Id = jsonData['weather'][0]['icon']
            icon1 = requests.get(f'http://openweathermap.org/img/wn/{icon}.png'.format(icon=icon_Id))
            icon_Data = icon1.content
            icon_detail = ImageTk.PhotoImage(Image.open(BytesIO(icon_Data)))
    
            # setting icons
            label1.config(text=setDesc)
            label2.config(text=setInfo)
            label3.config(image=icon_detail)
    
        except:
            label1.config(text="Error !! wrong City Name!")
            label2.config(text="")
            label3.config(text=" ")
    
    # tkinter intry box and label creation
    inputCity = Entry(root, width='15', justify='center', font=('Roboto', 15))
    inputCity.focus()
    inputCity.bind('<Return>', weather)
    inputCity.pack(pady="11")
    label1 = Label(font=("Raleway-MediumItalic", 15), background="#50514F", foreground="#F6E8EA")
    label1.pack(pady="10")
    
    label2 = Label(font=("Raleway-MediumItalic", 15), background="#50514F", foreground="#F6E8EA")
    label2.pack(pady="10")
    
    label3 = Label(background="#50514F")
    label3.pack(pady="10")
    
    mainloop()

I am trying to display the weather icon but unfortunately, it is not working. Please figure me out #where I am going wrong.

I am using open weather API for fetching icons and weather information.

I want to show the weather icon on the middle of the details.

Weather Application

bhucho
  • 3,903
  • 3
  • 16
  • 34
Vijay Sinha
  • 23
  • 1
  • 5
  • Does this answer your question? https://stackoverflow.com/questions/16424091 – Bryan Oakley Dec 03 '20 at 04:30
  • No, This is different... – Vijay Sinha Dec 08 '20 at 11:38
  • You definitely aren't saving a reference to the image as that other answer suggests so it's likely part of the problem. You're also assuming all errors are due to a bad city name, maybe some other error is being thrown. Finally, the posted code has several syntax errors which prevent it from being run. – Bryan Oakley Dec 08 '20 at 14:25
  • Thanks, Bryan but when there is a mistake in the city name it will not display the weather details. Thanks – Vijay Sinha Dec 10 '20 at 14:30
  • If you didn't ignore the error in the `except` block, maybe it would tell you what is going wrong. – Bryan Oakley Dec 10 '20 at 15:13

0 Answers0