Ну получается получить данные с OpenWeatherMap
Начал обучатся Python, c написания простых программ. Решил написать программу по погоде , с библиотекой Tkinter. Код работает, но выводится ошибка, из функции search().

Код самой программы. Основан на опыте западного видеоблогера
from tkinter import *
from tkinter import messagebox
from configparser import ConfigParser
import requests
config_file = 'config.ini'
config = ConfigParser()
config.read(config_file)
api_key = config['api_key']['key']
url = 'http://api.openweathermap.org/data/2.5/weather?q={}&appid={}'
def get_weather(city):
result = requests.get(url.format(city, api_key))
if result:
json = result.json()
# (City, Country, temp_celsius,temp_fahrenheit, icon, weather)
city = json["name"]
country = json["sys"]["country"]
temp_kelvin = json["main"]["temp"]
temp_celsius = temp_kelvin - 273.15
temp_fahrenheit = (temp_kelvin - 273.15) * 9/5 + 32
icon = json["weather"][0]["icon"]
weather = json["weather"][0]["main"]
final = (city, country, temp_celsius, temp_fahrenheit, icon, weather)
return final
else:
return None
def search():
city = city_text.get()
weather = get_weather(city)
if weather:
location_lbl['text'] = '{}, {}'.format(weather[0], weather[1])
image['bitmap'] = 'weather_icon/{}.png'.format(weather[4])
temp_lbl['text'] = '{:.2f}C, {:.2f}F'.format(weather[2], weather[3])
weather_lbl['text'] = weather[5]
else:
messagebox.showerror('Error', 'Cannot find city {}'.format(city))
app = Tk()
app.title("Test App")
app.geometry('400x350')
city_text = StringVar()
city_entry = Entry(app, textvariable = city_text)
city_entry.pack()
search_btn = Button(app, text = 'Search Weather', width = 12, command=search)
search_btn.pack()
location_lbl = Label(app, text = 'Location', font = ('bold', 20))
location_lbl.pack()
image = Label(app, bitmap ='')
image.pack()
temp_lbl = Label(app, text = '')
temp_lbl.pack()
weather_lbl = Label(app, text ='')
weather_lbl.pack()
app.mainloop()
Код Config.ini
[api_key]
key = '932d999dbd95894887b3ed90a9d65cc7'
Ответы (2 шт):
Проверь отрабатывает ли код после "if result:" Если да то смотри в "final" Если нет то проверь, правильно ли данные попали в requests.get и посмотри print(result)
ps код без apikey запустить не кто не сможет, если проложеш, то проще понять будет что к чему
не правильно осуществляется процедура обращения к файлу ini
config_file = 'config.ini'
config = ConfigParser()
config.read(config_file)
api_key = config.get('APIKeys', 'openweathermap')
в файле имеется неверный синтаксис
config.ini
[APIKeys]
openweathermap = 932d999dbd95894887b3ed90a9d65cc7
кроме того сайт, с которого осуществляется получение данных не имеет citi_IDI, а производит по данным широты и долготы местности. Программа работать в вашем формате осуществления запросов не будет.