-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather_gui.py
More file actions
48 lines (40 loc) · 1.24 KB
/
weather_gui.py
File metadata and controls
48 lines (40 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import tkinter as tk
from tkinter import messagebox
import requests
import os
from dotenv import load_dotenv
load_dotenv()
API_KEY = os.getenv("OPENWEATHER_API_KEY")
BASE_URL = "https://api.openweathermap.org/data/2.5/weather"
def get_weather(city):
params = {
'q': city,
'appid': API_KEY,
'units': 'metric'
}
response = requests.get(BASE_URL, params=params)
if response.status_code == 200:
data = response.json()
temp = data['main']['temp']
desc = data['weather'][0]['description']
return f"Temperature: {temp}°C\nDescription: {desc}"
else:
try:
error_data = response.json()
return f"Error: {error_data.get('message', error_data)}"
except Exception:
return f"HTTP Status: {response.status_code}"
def show_weather():
city = city_entry.get()
result = get_weather(city)
messagebox.showinfo("Weather Result", result)
root = tk.Tk()
root.title("WeatherApp GUI")
root.geometry("300x150")
city_label = tk.Label(root, text="Enter city name:")
city_label.pack(pady=5)
city_entry = tk.Entry(root)
city_entry.pack(pady=5)
get_btn = tk.Button(root, text="Get Weather", command=show_weather)
get_btn.pack(pady=10)
root.mainloop()