-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodsoft_taskno.4.py
57 lines (43 loc) · 1.53 KB
/
codsoft_taskno.4.py
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
49
50
51
52
53
54
55
56
57
import tkinter as tk
import requests
import tkinter.messagebox
def get_weather_data(location):
api_key = "ecb8f73dcc33bd561b915f25990e3559"
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {
"q": location,
"appid": api_key,
"units": "metric"
}
response = requests.get(base_url, params=params)
data = response.json()
if data["cod"] == 200:
weather_info = {
"Temperature": data["main"]["temp"],
"Humidity": data["main"]["humidity"],
"Wind Speed": data["wind"]["speed"],
"Description": data["weather"][0]["description"]
}
return weather_info
else:
return None
def get_weather():
location = entry_location.get()
weather_data = get_weather_data(location)
if weather_data:
weather_text.delete("1.0", tk.END)
for key, value in weather_data.items():
weather_text.insert(tk.END, f"{key}: {value}\n")
else:
tkinter.messagebox.showerror("Error", "Weather information not found. Please check the location.")
root = tk.Tk()
root.title("Weather Forecast")
label_location = tk.Label(root, text="Enter the name of a city or a zip code:")
entry_location = tk.Entry(root)
get_weather_button = tk.Button(root, text="Get Weather", command=get_weather)
weather_text = tk.Text(root, height=10, width=40)
label_location.pack()
entry_location.pack()
get_weather_button.pack()
weather_text.pack()
root.mainloop()