Build Project on Weather Forecast Application

Weather Forecast App

Hello coders we create this project with Python Tkinter,pytz,datetime,timezonefinder, geopy and requests Module

  •   In this project we create weather information program

Libraries used-

  >>Tkinter(for GUI)

  >>Pillow(for Backgroung Image)

  >>geopy (for weather info)

  >>timezonefinder (for time zone of given location)

  >>request (for sending request to API)

  >>pytz (for time of given location)

Step to create GUI-

  • Install Tkinter using pip install
  • Install Pillow using pip install
  • Import Tkinter
  • Create the main window
  • Import backgroung and button png
  • Create entry and buttons

Algorithm-

  • Create function of weather info.
  • Using geopy module detect weather info of given location.
  • Using timezonefinder detect  zone of given location.
  • Using request module send the request to geopy to get info.
  • Using pytz module detect time of given location.
  •  Create blank labels for outputs.
  • Create button over png and add command to it.

SourceCode:

#importing Libraries-------------------------------------------------------------------------------------------------------

from tkinter import *

import tkinter as tk

from PIL import ImageTk, Image

from geopy.geocoders import Nominatim

from tkinter import ttk,messagebox

from timezonefinder import TimezoneFinder

from datetime import datetime

import requests

import pytz



root=Tk()



#For putting Icon-----------------------------------------------------------------------------------------------------------

image_icon=PhotoImage(file="logo.png")

root.iconphoto(False,image_icon)



#For putting title----------------------------------------------------------------------------------------------------------

root.title("Weather App")



#For putting Image as background--------------------------------------------------------------------------------------------

root.geometry("1200x700+300+200")

root.resizable(False,False)

img = ImageTk.PhotoImage(Image.open("base.png"))

panel = Label(root, image = img)

panel.place(x=0,y=0)



#Function For Weather-------------------------------------------------------------------------------------------------------

def getWeather():

    city=textfield.get()



    geolocator=Nominatim(user_agent="geoapiExercises")

    location=geolocator.geocode(city)

    obj=TimezoneFinder()

    result=obj.timezone_at(lat=location.latitude,lng=location.longitude)



    home=pytz.timezone(result)

    local_time=datetime.now(home)

    current_time=local_time.strftime("%I:%M %p")

    clock.config(text=current_time)



    #Weather API

    api="https://api.openweathermap.org/data/2.5/weather?q="+city+"&appid=462fde2b41fc637e0fe5fe2eee61525e"



    json_data=requests.get(api).json()

    condition=json_data['weather'][0]['main']

    description=json_data['weather'][0]['description']

    temp=int(json_data['main']['temp']-273.15)

    pressure=json_data['main']['pressure']

    humidity=json_data['main']['humidity']

    wind=json_data['wind']['speed']



    t.config(text=(temp,"°"))

    c.config(text=(condition,"|","Feels","Like",temp,"°"))



    w.config(text=wind)

    h.config(text=humidity)

    d.config(text=pressure)

    p.config(text=description)  



#For Search Box-------------------------------------------------------------------------------------------------------------

textfield=tk.Entry(root,justify="left",width=18,font=("Times New Roman",30,"bold"),fg="white",bg="#005078",bd=0)

textfield.place(x=560,y=87)

textfield.insert(0,"Enter Your Location")



def click(event):

    textfield.delete(0,END)

    textfield.insert(0,'')

textfield.bind('<FocusIn>',click)





#For Search Button----------------------------------------------------------------------------------------------------------

search_icon=PhotoImage(file="search.png")

myimage_icon=Button(image=search_icon,bd=0,cursor="hand2",bg="#005078",command=getWeather)

myimage_icon.place(x=925,y=85)



#For Time-------------------------------------------------------------------------------------------------------------------

clock=Label(root,font=("Times New Roman",30,"bold"),bg="#009CC4",fg="white")

clock.place(x=70,y=120)



#For Weather Details--------------------------------------------------------------------------------------------------------

t=Label(font=("Times New Roman",120,"bold"),fg="white",bg="#009CC4")

t.place(x=750,y=150)



c=Label(font=("Times New Roman",40,"bold"),fg="white",bg="#009CC4")

c.place(x=600,y=350)



w=Label(text=".......",font=("Times New Roman",20,"bold"),bg="#015079",fg="white")

w.place(x=175,y=550)



h=Label(text=".......",font=("Times New Roman",20,"bold"),bg="#005F8C",fg="white")

h.place(x=575,y=550)



p=Label(text=".......",font=("Times New Roman",20,"bold"),justify="center",bg="#005F8C",fg="white")

p.place(x=575,y=650)



d=Label(text=".......",font=("Times New Roman",20,"bold"),bg="#015079",fg="white")

d.place(x=960,y=550)



root.mainloop()


Output

Write a comment