Skip to content

Instantly share code, notes, and snippets.

@Estherns
Last active January 17, 2020 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Estherns/97ff58f8dc641218deda604e17b66f7b to your computer and use it in GitHub Desktop.
Save Estherns/97ff58f8dc641218deda604e17b66f7b to your computer and use it in GitHub Desktop.
A program to get weather condition of a any city in the world
#import tkinter library
import tkinter as tk
import requests
import tkinter.messagebox
#set the height for the canvas
HEIGHT = 500
WIDTH = 600
#A function to display the weather
def format_response(weather):
try:
name = weather['name']
desc = weather['weather'][0]['description']
temp = weather['main']['temp']
details_str = 'City: %s \nConditions: %s \nTemperature (°F): %s' % (name, desc, temp)
except:
details_str = 'Sorry, it seems we cannot find that city. \nCheck if you typed the correct city name.'
return details_str
# a function to get weather acording to the cities. Note, we get the API key from openweathermap
def get_weather(city):
weather_key = '5a835f35f03c205b1d1fcdec14ce7fec'
url = 'https://api.openweathermap.org/data/2.5/weather'
params = {'APPID': weather_key, 'q': city, 'units': 'imperial'}
response = requests.get(url, params=params)
weather = response.json()
label['text'] = format_response(weather)
Conditions = weather['weather'][0]['description']
if Conditions == "cloudy":
tkinter.messagebox.showinfo("Weather", "Carry an umbrella and keep warm")
elif Conditions == "few clouds":
tkinter.messagebox.showinfo("Weather", "Carry an umbrella just incase it rains")
elif Conditions == "scattered clouds":
tkinter.messagebox.showinfo("Weather", "Good weather, you can have fun")
elif Conditions == "overcast clouds":
tkinter.messagebox.showinfo("Weather", "Enjoy yourself")
elif Conditions == "clear sky":
tkinter.messagebox.showinfo("Weather", "Brace yourself for high temperatures")
root = tk.Tk()
root.title('My Weather App')
root.iconbitmap('weatherBg.png')
#format our display by setting the size, background color and fonts
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
background_image = tk.PhotoImage(file='weatherBg.png')
background_label = tk.Label(root, image=background_image)
background_label.place(relwidth=1, relheight=1)
frame = tk.Frame(root, bg='#036C08', bd=5)
frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')
entry = tk.Entry(frame, font=('valera', 15))
entry.place(relwidth=0.70, relheight=1)
button = tk.Button(frame, text="Check Weather", bg='#036C08', font=('valera', 12), fg='white', command=lambda: get_weather(entry.get()))
button.place(relx=0.7, relheight=1, relwidth=0.3)
display_frame = tk.Frame(root, bg='#036C08', bd=10)
display_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.5, anchor='n')
label = tk.Label(display_frame, font=('valera', 18), anchor ='nw', justify ='left', bd=4)
label.place(relwidth=1, relheight=1)
root.mainloop()
@Estherns
Copy link
Author

The weather app is a project that checks the weather on any city in the world and return
the weather condition based on the query, and gives a recommendation on what to do.

I intend to add more functionality so as to solve the emerging climate problems to the world.
For instance the uncontrollable bush fires in Australia, floods and landslides in Kenya, Malawi, droughts in many parts of sub-Sahara region, among many others.

I welcome any input or suggestion to make this project a success.

Below is a screenshot of a sample weather data for Nairobi, Kenya.

weatherapp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment