Weather Forecasting in Python
Before pasting the code in IDE you need to Install Requests in Terminal (if you Don't know how to install see this video):
SOURCE CODE (WEATHER FORECASTING)
import tkinter as tk
import requests # pip install requests
import time
def getWeather(canvas):
city = textField.get()
api = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=06c921750b9a82d8f5d1294e1586276f"
json_data = requests.get(api).json()
condition = json_data['weather'][0]['main']
temp = int(json_data['main']['temp'] - 273.15)
min_temp = int(json_data['main']['temp_min'] - 273.15)
max_temp = int(json_data['main']['temp_max'] - 273.15)
pressure = json_data['main']['pressure']
humidity = json_data['main']['humidity']
wind = json_data['wind']['speed']
sunrise = time.strftime('%I:%M:%S', time.gmtime(json_data['sys']['sunrise'] - 21600))
sunset = time.strftime('%I:%M:%S', time.gmtime(json_data['sys']['sunset'] - 21600))
final_info = condition + "\n" + str(temp) + "°C"
final_data = "\n" + "Min Temp: " + str(min_temp) + "°C" + "\n" + "Max Temp: " + str(
max_temp) + "°C" + "\n" + "Pressure: " + str(pressure) + "\n" + "Humidity: " + str(
humidity) + "\n" + "Wind Speed: " + str(wind) + "\n" + "Sunrise: " + sunrise + "\n" + "Sunset: " + sunset
label1.config(text=final_info)
label2.config(text=final_data)
canvas = tk.Tk()
canvas.geometry("600x500")
canvas.title("Weather App")
f = ("poppins", 15, "bold")
t = ("poppins", 35, "bold")
textField = tk.Entry(canvas, justify='center', width=20, font=t)
textField.pack(pady=20)
textField.focus()
textField.bind('<Return>', getWeather)
label1 = tk.Label(canvas, font=t)
label1.pack()
label2 = tk.Label(canvas, font=f)
label2.pack()
canvas.mainloop()
OUTPUT:
Hey, guys Checkout My Brothers New App(Java Programmer) :
This App is useful for Guys who need to learn Java from the Beginning.
App Features:
1.Interactive User interface
2.Neat and clear layout for better visibility
3.Many programs with clear output
4.Topic-wise Programs
5.Topic-wise theory with full description
6.Standard interview questions and answers
7.Very Simple and Understandable language
Follow my Brother on Instagram for More useful Application updates :
IN CASE OF ANY DOUBT CONTACT ME THROUGH INSTAGRAM (LINK GIVEN BELOW)
FOLLOW US ON INSTAGRAM FOR MORE PROJECTS AND UPDATES: https://www.instagram.com/pythonbeginner13/
Comments
Post a Comment