Advertisement
plarmi

weather1

May 30th, 2024
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. # Создать оконное приложение. Сделать так, чтобы с помощью API от openweathermap в окне выводилась
  2. # информация о погоде в городе, название которого вводится в поле для ввода. Нужно сделать так,
  3. # чтобы всё было на русском языке. Скомпилировать программу, exe файл вместе с исходным кодом загрузить на Github
  4.  
  5. from tkinter import *
  6. import requests
  7. from PIL import Image, ImageTk
  8.  
  9. def save_image(url):
  10.     response = requests.get(url)
  11.     with open("weather_icon.png", "wb") as file:
  12.         file.write(response.content)
  13.     image = Image.open("weather_icon.png")
  14.     return ImageTk.PhotoImage(image)
  15.  
  16. def show_weather():
  17.     response = requests.get(f"https://api.openweathermap.org/data/2.5/weather?q={weather_entry.get()}&appid=3faa8133371792ef1aacd11949472dc8").json()
  18.     img = save_image(f"https://openweathermap.org/img/wn/{response["weather"][0]["icon"]}@2x.png")
  19.     picture.config(image=img, background="cyan")
  20.     picture.image = img
  21.     weather_info.config(text=f"{(response["weather"][0]["description"]).capitalize()}\n"
  22.                              f"Температура: {round(response["main"]["temp"] - 273.15)} °C\n"
  23.                              f"Скорость ветра: {response["wind"]["speed"]} м/c")
  24.  
  25.  
  26. window = Tk()
  27. window.geometry("500x500")
  28. window.title("Прогноз погоды")
  29. window.resizable(False, False)
  30.  
  31. welcome_text = Label(window, text="Прогноз погоды", font=("Times New Roman", 40, "bold"))
  32. welcome_text.pack()
  33.  
  34. weather_entry = Entry(window)
  35. weather_entry.pack()
  36. weather_entry.focus()
  37.  
  38. btn = Button(window, text="Показать погоду", command=show_weather)
  39. btn.pack(pady=10)
  40.  
  41. picture = Label(window)
  42. picture.pack()
  43.  
  44. weather_info = Label(window, font=("Times New Roman", 30))
  45. weather_info.pack()
  46.  
  47.  
  48. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement