GK SCIENTIST

General Knowledge One Stop Source
Menu
  • Home
  • Social Science
    • History
    • Political Science
    • Geography
  • Science
    • Physics
    • Chemistry
    • Biology
  • Chemistry Notes
  • Mathematics
  • Computer
  • Tutorial MySQL
  • Tutorial Python
  • Java Tutorial
  • English Grammar
  • English Essay
  • Indian Anthropology
  • Philosophy
  • Solved Paper
  • UPSC
  • Current Content
    • Current Affairs
    • RSTV News
    • Yojana and Kurukshetra Gist
  • Donate
  • Contact Us

If you are interested in advertising to our audience, submit the advertising enquiry form.

Advertising Enquiry
Computer

Create Weather App in Python

Gk Scientist December 19, 2022 No Comments
Tweet WhatsApp Telegram

Create Weather App in Python:

We can use the PyCharm code editor for this example. If you do not know about it then follow this link- How to install PyCharm for Python and create a program in it.

(1) Open the PyCharm code editor and create a new project in it. We named the project weather_app.py. To build a weather app in Python, we need to install some packages. For this open the terminal in PyCharm and install three packages i.e. geopy, timezonefinder, and pytz. You install all packages at the same time by simply typing in the terminal following command i.e. pip install geopy timezonefinder pytz and then press enter as shown below.

weather app package install in python - Create Weather App in Python

Note: If any package shows an error while using in the code, you can simply install the package by clicking on the red bulb which appears below the error and you can directly install the package from here as shown below.

import geopy in pycharm - Create Weather App in Python

(2) Now add some images in the path where you have created the project as shown below.

add weather app images python - Create Weather App in Python

Download Images from Here: Weather App Images.

(3) Now go to this website Weather API – OpenWeatherMap and create an account here and get the API key as shown.

create account on waether api - Create Weather App in Python
give company name in weather api - Create Weather App in Python
weather app api key - Create Weather App in Python
api doc weather app - Create Weather App in Python
make an api call - Create Weather App in Python

(4) To create a Weather App in Python write the following code as shown below.

python weather app code unit 1 - Create Weather App in Python
python weather app code unit 2 - Create Weather App in Python
python weather app code 3 - Create Weather App in Python
python weather app code 4 - Create Weather App in Python

Weather App in Python Source Code: Do proper Code Indentation otherwise, the Source Code does not work and gives an error.

from tkinter import *
import tkinter as tk
from geopy.geocoders import Nominatim
from tkinter import ttk,messagebox
from timezonefinder import TimezoneFinder
from datetime import datetime
import requests
import pytz

root=Tk()
root.title("Weather App")
root.geometry("800x400+200+100")
root.resizable(False,False)

def getWeather():
 try:
    city=text_field.get()
    geolocator=Nominatim(user_agent="geoapiExercises")
    location=geolocator.geocode(city)
    obj=TimezoneFinder()
    result=obj.timezone_at(lng=location.longitude,lat=location.latitude)
    print(result)

    home=pytz.timezone(result)
    local_time=datetime.now(home)
    current_time=local_time.strftime("%I:%M %p")
    clock.config(text=current_time)
    name.config(text="CURRENT WEATHER")

# Add API Key Weather
    api="https://api.openweathermap.org/data/2.5/weather?q="+city+"&appid=b22d362851862b"
    json_data=requests.get(api).json()
    t.config(text=str(json_data['main']['temp'] - 273.15))
    c.config(text=json_data['weather'][0]['main'])
    w.config(text=json_data['wind']['speed'])
    h.config(text=json_data['main']['humidity'])
    d.config(text=json_data['weather'][0]['description'])
    p.config(text=json_data['main']['pressure'])

 except Exception as e:
     messagebox.showerror("Weather App","Invalid Entry!!")

#Add Search Bar
image_search=PhotoImage(file="search_bar.png")
searchbar_image=Label(image=image_search)
searchbar_image.place(x=10,y=10)
text_field=tk.Entry(root,justify="center",width=17,font=("poppins",18,"bold"),bg="#147886",border=0,fg="white")
text_field.place(x=20,y=20)
text_field.focus()

#Add Search Icon
image_search_icon=PhotoImage(file="search_icon.png")
search_icon=Button(image=image_search_icon,borderwidth=0,cursor="hand2",bg="#147886",command=getWeather)
search_icon.place(x=250,y=18)

#Add Weather Logo
image_logo=PhotoImage(file="weather_logo.png")
weather_logo=Label(image=image_logo)
weather_logo.place(x=250,y=90)

#Add Information Box
image_box=PhotoImage(file="information_box.png")
information_box=Label(image=image_box)
information_box.pack(padx=5,pady=5,side=BOTTOM)

#Time
name=Label(root,font=("arial",15,"bold"))
name.place(x=30,y=100)
clock=Label(root,font=("Merriweather",20))
clock.place(x=30,y=130)

#Label
label1=Label(root,text="WIND",font=("Merriweather",15,"bold"),fg="White",bg="#5AC9D9")
label1.place(x=100,y=330)

label2=Label(root,text="HUMIDITY",font=("Merriweather",15,"bold"),fg="White",bg="#5AC9D9")
label2.place(x=210,y=330)

label3=Label(root,text="DESCRIPTION",font=("Merriweather",15,"bold"),fg="White",bg="#5AC9D9")
label3.place(x=360,y=330)

label4=Label(root,text="PRESSURE",font=("Merriweather",15,"bold"),fg="White",bg="#5AC9D9")
label4.place(x=570,y=330)

t=Label(font=("arial",50,"bold"),fg="#ee666d")
t.place(x=400,y=150)
c=Label(font=("arial",20,"bold"))
c.place(x=420,y=230)

w=Label(text="...",font=("arial",16,"bold"),bg="#5AC9D9")
w.place(x=110,y=360)
h=Label(text="...",font=("arial",16,"bold"),bg="#5AC9D9")
h.place(x=230,y=360)
d=Label(text="...",font=("arial",16,"bold"),bg="#5AC9D9")
d.place(x=370,y=360)
p=Label(text="...",font=("arial",16,"bold"),bg="#5AC9D9")
p.place(x=590,y=360)

root.mainloop()

(5) Now run the code and a window pop up, enter the city name or country name and get the output as shown below.

python weather app works - Create Weather App in Python

Tutorial MySQL
Tutorial Python
Email (Electronic Mail)
World Wide Web (WWW)
Secondary Storage Devices
Applications of Computer
Prev Article
Next Article

Related Articles

Python MySQL Self Join and Full Join
Python MySQL Self Join and Full Join: (1) Self Join- …
Gk Scientist December 15, 2022 Computer

Python MySQL Self Join and Full Join

Rock Paper Scissors Game in Python (Using Random Module)
Rock Paper Scissors Game in Python: We can use the …
Gk Scientist December 3, 2022 Computer

Rock Paper Scissors Game in Python (Using Random Module)

Online Email Marketing | Best Email Marketing Tools
Online Email Marketing: Online email marketing is the practice of …
Gk Scientist January 24, 2023 Computer

Online Email Marketing | Best Email Marketing Tools

Email (Electronic Mail)
Email: Electronic Mail is sending and receiving messages by a …
Gk Scientist September 16, 2022 Computer

Email (Electronic Mail)

Python Digital Clock Project
Python Digital Clock Project: We can use the PyCharm code …
Gk Scientist December 24, 2022 Computer

Python Digital Clock Project

Utility Software
What is Utility Software?  Utility programs also known as service …
Gk Scientist September 30, 2022 Computer

Utility Software

Sets in Python
Sets in Python: We can use the PyCharm code editor …
Gk Scientist December 2, 2022 Computer

Sets in Python

Python Dice Roll Simulator Project
Python Dice Roll Simulator: We can use the PyCharm code …
Gk Scientist December 21, 2022 Computer

Python Dice Roll Simulator Project

Leave a Reply Cancel Reply

Search

  • Popular
  • Recent




GK SCIENTIST

General Knowledge One Stop Source

Information

  • About Us
  • Terms and Condition, Disclaimer
  • Contact Us

Android Apps

  • IAS App For English Medium Students
  • IAS Basics App For English Medium Students
  • IAS Hindi App For Hindi Medium Students
DMCA.com Protection Status

Popular Tags

Biology (33) Biology Questions (88) Chemistry (57) Computer (215) Current Affairs (4) Current Content (0) Economy (15) English Essay (172) English Grammar (75) English Literature (10) Geography (83) History (259) Indian Anthropology (11) Indian Polity (14) JKAS Mains Question Papers (17) Mathematics (68) Moral Science (7) NCERT & Other Boards Books (25) Philosophy (114) Physics (89) Political Science (132) RS TV News (33) Science (553) Social Anthropology (7) Social Science (17) Solved Paper (47) UPSC (7) UPSC Mains Question Papers (26)

Downloads

  • NCERT Books
  • Old NCERT Books
  • NIOS Books For IAS, SSC, and State PSC Exam
  • Tamil Nadu Board Books: Important For UPSC, SSC, and State PSC Exam
  • Modern Indian and World History Notes For IAS Exam
  • UPSC Topper 2013 Gaurav Agrawal Notes For IAS Preparation
  • UPSC IAS Prelims General Studies – Previous Year Papers
  • UPSC Mains Question Papers

Copyright © 2023 GK SCIENTIST
Theme by MyThemeShop.com& Hosted On Cloudways

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh
 

Loading Comments...