Skip to content

techgitp/weather

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

import requests import tkinter as tk from tkinter import messagebox, StringVar, ttk

def get_weather(api_key, location, units): url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units={units}" response = requests.get(url) if response.status_code == 200: data = response.json() temperature = data['main']['temp'] humidity = data['main']['humidity'] wind_speed = data['wind']['speed'] weather_description = data['weather'][0]['description'] return temperature, humidity, wind_speed, weather_description else: return None

def show_weather(): api_key = '4f65f4882975c3ae801acc971fa20057' # Replace with your actual API key location = entry.get() units = unit_var.get()

if not location:
    messagebox.showerror("Error", "Please enter a location.")
    return

weather_data = get_weather(api_key, location, units)
if weather_data:
    temperature, humidity, wind_speed, description = weather_data
    unit_symbol = "°C" if units == "metric" else "°F"
    result_label.config(text=f"Temperature: {temperature}{unit_symbol}\n"
                             f"Humidity: {humidity}%\n"
                             f"Wind Speed: {wind_speed} m/s\n"
                             f"Weather: {description.capitalize()}")
else:
    messagebox.showerror("Error", "Could not retrieve weather data. Please check your input.")

def clear_fields(): entry.delete(0, tk.END) result_label.config(text="")

GUI Setup

root = tk.Tk() root.title("Weather App")

Input Frame

input_frame = tk.Frame(root) input_frame.pack(pady=10)

entry = tk.Entry(input_frame, width=30) entry.pack(side=tk.LEFT, padx=5)

search_button = tk.Button(input_frame, text="Get Weather", command=show_weather) search_button.pack(side=tk.LEFT, padx=5)

clear_button = tk.Button(input_frame, text="Clear", command=clear_fields) clear_button.pack(side=tk.LEFT, padx=5)

Unit Selection

unit_var = StringVar(value="metric") unit_frame = tk.Frame(root) unit_frame.pack(pady=5)

metric_radio = tk.Radiobutton(unit_frame, text="Celsius", variable=unit_var, value="metric") metric_radio.pack(side=tk.LEFT)

imperial_radio = tk.Radiobutton(unit_frame, text="Fahrenheit", variable=unit_var, value="imperial") imperial_radio.pack(side=tk.LEFT)

Result Label

result_label = tk.Label(root, text="", justify=tk.LEFT) result_label.pack(pady=10)

root.mainloop()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors