Вопрос : получить значки погоды из openweathermap
Согласно комментариям:
Значение значка"03N". Конечно, tkinter не знает, как это показать. В документации openweathermap вы должны увидеть, как получить этот значок. - @ zvone
см. SO: Ответ установить значок приложения tkinter с URL-адресом? , чтобы получить значок.
используйте f'http://openweathermap.org/img/wn/{icon}.png'
в качестве URL-адреса. - @ Stef

import tkinter as tk
import requests, base64
class OpenWeatherMap:
APPID = 'c73d9cdb31fd6a386bee66158b116cd0'
def __init__(self):
self.url = "http://api.openweathermap.org/data/2.5/weather?appid={appid}&q={city}&units=metric"
self.json = {}
def get_city(self, city):
url = self.url.format(appid=OpenWeatherMap.APPID, city=city)
self.json = requests.get(url).json()
return self.json
def get(self, key):
return self.json['main'][key]
def get_icon_data(self):
icon_id = self.json['weather'][0]['icon']
url = 'http://openweathermap.org/img/wn/{icon}.png'.format(icon=icon_id)
response = requests.get(url, stream=True)
return base64.encodebytes(response.raw.read())
class OWIconLabel(tk.Label):
def __init__(self, parent, **kwargs):
weather_icon = kwargs.pop('weather_icon', None)
if weather_icon is not None:
self.photo = tk.PhotoImage(data=weather_icon)
kwargs['image'] = self.photo
super().__init__(parent, **kwargs)
Использование :
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry("220x120+0+0")
self.configure(bg='black')
owm = OpenWeatherMap()
owm.get_city('karachi')
temperature = owm.get('temp')
temp_icon = OWIconLabel(self, weather_icon=owm.get_icon_data())
temp_icon.grid(row=0, column=0)
self.temp = tk.Label(self,
text='{} deg celcius'.format(temperature),
font=("Helvetica", 15), bg='black', fg='white')
self.temp.grid(row=1, column=0)
if __name__ == '__main__':
App().mainloop()
Проверено на Python: 3,5