Код для приложения (Прогноз погоды).
- 5
- 0
- Опубликовано: 2024-02-12
import requests
import tkinter as tk
# Создание главного окна приложения
root = tk.Tk()
root.title("Прогноз погоды")
# Создание поля для ввода города
city_label = tk.Label(root, text="Введите город:")
city_label.grid(row=0, column=0)
city_entry = tk.Entry(root)
city_entry.grid(row=0, column=1)
# Создание функции для получения прогноза погоды
def get_weather():
# Получение города из поля ввода
city = city_entry.get()
# Создание запроса к API OpenWeatherMap
url = "https://api.openweathermap.org/data/2.5/weather?q={}&appid={}&units=metric".format(city, "ваш_api_ключ")
response = requests.get(url)
# Извлечение данных из ответа API
data = response.json()
weather_description = data["weather"][0]["description"]
temperature = data["main"]["temp"]
# Отображение прогноза погоды в окне приложения
weather_label = tk.Label(root, text="Погода в городе {}:".format(city))
weather_label.grid(row=1, column=0)
description_label = tk.Label(root, text=weather_description)
description_label.grid(row=2, column=0)
temperature_label = tk.Label(root, text="Температура: {}°C".format(temperature))
temperature_label.grid(row=3, column=0)
# Создание кнопки для получения прогноза погоды
get_weather_button = tk.Button(root, text="Получить прогноз", command=get_weather)
get_weather_button.grid(row=4, column=0)
# Запуск главного цикла приложения
root.mainloop()
```