import tkinter as tk
from tkinter import messagebox
import requests

def fetch_weather():
    location = location_entry.get()
    if not location:
        messagebox.showerror("Error", "Please enter a location.")
        return
    
    api_key = "1c7d4551a4f4f58a2f3e5e09f1ebb062"
    url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=metric"
    response = requests.get(url)
    data = response.json()
    
    if response.status_code == 200:
        weather_info = f"Weather in {data['name']}:\n"
        weather_info += f"Temperature: {data['main']['temp']}°C\n"
        weather_info += f"Humidity: {data['main']['humidity']}%\n"
        weather_info += f"Wind Speed: {data['wind']['speed']} m/s"
        messagebox.showinfo("Weather Information", weather_info)
    else:
        messagebox.showerror("Error", f"Failed to fetch weather data for {location}. Please check the location.")


root = tk.Tk()
root.title("Weather App")


frame = tk.Frame(root)
frame.pack(padx=10, pady=10)

location_label = tk.Label(frame, text="Enter city name or zip code:")
location_label.grid(row=0, column=0)

location_entry = tk.Entry(frame, width=30)
location_entry.grid(row=0, column=1)

fetch_button = tk.Button(frame, text="Fetch Weather", command=fetch_weather)
fetch_button.grid(row=1, columnspan=2)


root.mainloop()