main.py
get_weather() gets the weather info from the OpenWeatherApp Api website.
def get_weather(city):
'''gets weather info from OpenWeatherMap API'''
API_key = "***********************"
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_key}"
res = requests.get(url)
if res.status_code == 404:
messagebox.showerror("Error", "City not found")
return None
# Parse response JSON to get weather info
weather = res.json()
icon_id = weather['weather'][0]['icon']
temperature = weather['main']['temp'] - 273.15
it also gets the temperature and icon to make it into readable text.
search() allows the user to search for the current weather of a city.
mian.py
def search():
'''Searches the current weather of a city'''
city = city_entry.get()
result = get_weather(city)
if result is None:
return
The rest is the set up for the GUI.
window = ttkbootstrap.Window(themename="morph")
window.title("Weather App")
window.geometry("400x400")
# Entry widget - enter city name
city_entry = ttkbootstrap.Entry(window, font="Helvetica, 18")
city_entry.pack(pady=10)
# Button widget - search for weather information
search_button = ttkbootstrap.Button(window, text="Search",
command=search, bootstyle="warning")
search_button.pack(pady=10)
# label widget - show country/city name
location_label = tk.Label(window, font="Helvetica, 25")
location_label.pack(pady=20)
# Label widget - show weather icon
icon_label = tk.Label(window)
icon_label.pack()
# Label widget - show the temperature
temperature_label = tk.Label(window, font="Helvetica, 20")
temperature_label.pack()
# Label widget - show weather desc.
desc_label = tk.Label(window, font="Helvetica, 20")
desc_label.pack()
window.mainloop()
That’s really all I did… 1. because I started a little late and 2. it took too long for me to get an account for the API key