[Python] Can't fetch weather icons from openweathermap

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I have been trying to fetch weather icons from openweathermap api for a python application, but for some reason I am not able to do so.

    I am however, able to print the icon as text for e.g. its code like "3d" or "3n" but not the image.

    When I try to print the image, the error _tkinter.TclError: image "03n" doesn't exist pops up.

    My code is below.

    import requests
    from tkinter import *

    root = Tk()
    root.geometry("250x250+0+0")
    root.configure(bg='black')
    url = 'http://api.openweathermap.org/data/2.5/weather?appid=c73d9cdb31fd6a386bee66158b116cd0&q=karachi&units=metric'

    json = requests.get(url).json()
    temperature = json['main']['temp']
    icon = json['weather'][0]['icon']
    #icon1 = 'http://openweathermap.org/img/wn/10d@2x.png'
    #print(icon)
    root.lab1 = Label(root,image=icon)
    root.lab1.pack(side=TOP)
    root.lab= Label(text=(root,'{} deg celcius'.format(temperature)),font=("Helvetica 15"), bg='black', fg='white')
    root.lab.pack(side=LEFT)

    Continue reading...

Compartilhe esta Página