Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Создать оконное приложение. Сделать так, чтобы с помощью API от openweathermap в окне выводилась
- # информация о погоде в городе, название которого вводится в поле для ввода. Нужно сделать так,
- # чтобы всё было на русском языке. Скомпилировать программу, exe файл вместе с исходным кодом загрузить на Github
- from tkinter import *
- import requests
- from PIL import Image, ImageTk
- def save_image(url):
- response = requests.get(url)
- with open("weather_icon.png", "wb") as file:
- file.write(response.content)
- image = Image.open("weather_icon.png")
- return ImageTk.PhotoImage(image)
- def show_weather():
- response = requests.get(f"https://api.openweathermap.org/data/2.5/weather?q={weather_entry.get()}&appid=3faa8133371792ef1aacd11949472dc8").json()
- img = save_image(f"https://openweathermap.org/img/wn/{response["weather"][0]["icon"]}@2x.png")
- picture.config(image=img, background="cyan")
- picture.image = img
- weather_info.config(text=f"{(response["weather"][0]["description"]).capitalize()}\n"
- f"Температура: {round(response["main"]["temp"] - 273.15)} °C\n"
- f"Скорость ветра: {response["wind"]["speed"]} м/c")
- window = Tk()
- window.geometry("500x500")
- window.title("Прогноз погоды")
- window.resizable(False, False)
- welcome_text = Label(window, text="Прогноз погоды", font=("Times New Roman", 40, "bold"))
- welcome_text.pack()
- weather_entry = Entry(window)
- weather_entry.pack()
- weather_entry.focus()
- btn = Button(window, text="Показать погоду", command=show_weather)
- btn.pack(pady=10)
- picture = Label(window)
- picture.pack()
- weather_info = Label(window, font=("Times New Roman", 30))
- weather_info.pack()
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement