Python
import tkinter as tk from tkinter import messagebox import requests def get_weather(): api_key = 'YOUR_API_KEY' base_url = 'http://api.openweathermap.org/data/2.5/weather' city = city_entry.get() if not city: messagebox.showerror('Error', 'Please enter a city name') return params = {'q': city, 'appid': api_key, 'units': 'metric'} response = requests.get(base_url, params=params) weather_data = response.json() if weather_data['cod'] != '404': main_info = weather_data['weather'][0]['main'] description = weather_data['weather'][0]['description'] temperature = weather_data['main']['temp'] humidity = weather_data['main']['humidity'] wind_speed = weather_data['wind']['speed'] messagebox.showinfo('Weather Forecast', f'Main: {main_info}\n' f'Description: {description}\n' f'Temperature: {temperature}°C\n' f'Humidity: {humidity}%\n' f'Wind Speed: {wind_speed} m/s') else: messagebox.showerror('Error', 'City not found') # Create the main window root = tk.Tk() root.title('Weather Forecast') # Create and pack the city entry widget city_entry = tk.Entry(root) city_entry.pack() # Create and pack the submit button submit_button = tk.Button(root, text='Get Weather', command=get_weather) submit_button.pack() # Run the application root.mainloop()Make sure to replace
'YOUR_API_KEY'
with your actual OpenWeatherMap API key. You can sign up for a free API key on the OpenWeatherMap website.This code creates a simple GUI with a text entry field for the city name and a button to retrieve the weather forecast. When the button is clicked, the
get_weather
function is called, which sends a request to the OpenWeatherMap API to get the weather information for the specified city. The weather data is then displayed in a message box.Note that this code is just a basic example and can be further enhanced and customized according to your needs