Wapp
import requests
import json
import tkinter as tk
def get_weather(city):
api_key = "YOUR_API_KEY" # Replace with your actual API key
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {
"q": city,
"appid": api_key,
"units": "metric"
}
response = requests.get(base_url, params=params)
data = json.loads(response.text)
if data["cod"] != "404":
main_info = data["weather"][0]["main"]
description = data["weather"][0]["description"]
temperature = data["main"]["temp"]
humidity = data["main"]["humidity"]
wind_speed = data["wind"]["speed"]
result_text = f"Weather in {city}:\n"
result_text += f"Main: {main_info}\n"
result_text += f"Description: {description}\n"
result_text += f"Temperature: {temperature}°C\n"
result_text += f"Humidity: {humidity}%\n"
result_text += f"Wind Speed: {wind_speed} m/s"
result_label.config(text=result_text)
else:
result_label.config(text="City not found.")
def get_weather_button_click():
city = city_entry.get()
get_weather(city)
# Create the main window
window = tk.Tk()
window.title("Weather App")
# Create and configure the widgets
city_label = tk.Label(window, text="Enter city name:")
city_label.pack()
city_entry = tk.Entry(window)
city_entry.pack()
get_weather_button = tk.Button(window, text="Get Weather", command=get_weather_button_click)
get_weather_button.pack()
result_label = tk.Label(window, text="")
result_label.pack()
# Start the main event loop
window.mainloop()
Comments
Post a Comment