├── .gitignore ├── README.md ├── main.py └── requiment.txt /.gitignore: -------------------------------------------------------------------------------- 1 | venv 2 | .vscode 3 | __pycache__ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Openweather Weather App 2 | 3 | ## My first python application. 4 | 5 | Once you run the app, enter the city you would like to get the weather for and press the 'Get Weather" button. -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from urllib.request import urlopen 2 | from urllib.error import HTTPError 3 | from tkinter import messagebox 4 | import urllib.parse 5 | import tkinter as tk 6 | import os 7 | import json 8 | import time 9 | 10 | weather_Dict = { 11 | "currentTemp": "", 12 | "sunrise": "", 13 | "sunset": "", 14 | "lowTemp": "", 15 | "highTemp": "", 16 | } 17 | 18 | data = {} 19 | 20 | key = os.environ.get("OPENWEATHER_API") 21 | 22 | 23 | def kelvin_to_farenheit_conversion(temperature): 24 | converted = (temperature - 273.15) * 9 / 5 + 32 25 | return int(converted) 26 | 27 | 28 | def unix_to_local_time_conversion(api_time): 29 | return time.strftime("%I:%M %p", time.localtime(int(api_time))) 30 | 31 | 32 | def populate_weather(city_name): 33 | 34 | url_encoded_city_name = urllib.parse.quote(city_name) 35 | 36 | try: 37 | url = f"http://api.openweathermap.org/data/2.5/weather?q={url_encoded_city_name}&appid={key}" 38 | 39 | with urlopen(url) as response: 40 | source = response.read() 41 | 42 | except HTTPError: 43 | tk.messagebox.showwarning( 44 | title="Warning", message="Check your city name and try again." 45 | ) 46 | except Exception as e: 47 | tk.messagebox.showwarning(title="Warning", message=f"Something went wrong! {e}") 48 | else: 49 | 50 | data = json.loads(source) 51 | 52 | weather_Dict["currentTemp"] = kelvin_to_farenheit_conversion( 53 | data["main"]["temp"] 54 | ) 55 | weather_Dict["sunrise"] = unix_to_local_time_conversion( 56 | int(data["sys"]["sunrise"]) 57 | ) 58 | weather_Dict["sunset"] = unix_to_local_time_conversion( 59 | int(data["sys"]["sunset"]) 60 | ) 61 | weather_Dict["lowTemp"] = kelvin_to_farenheit_conversion( 62 | data["main"]["temp_min"] 63 | ) 64 | weather_Dict["highTemp"] = kelvin_to_farenheit_conversion( 65 | data["main"]["temp_max"] 66 | ) 67 | 68 | sunrise_label = tk.Label( 69 | master=root, text=f"Sunrise will be {weather_Dict['sunrise']}", padx=10 70 | ) 71 | sunrise_label.grid(row=1, column=0, columnspan=6, sticky=tk.W) 72 | 73 | sunset_label = tk.Label( 74 | master=root, text=(f"Sunset will be {weather_Dict['sunset']}"), padx=10 75 | ) 76 | sunset_label.grid(row=2, column=0, columnspan=6, sticky=tk.W) 77 | 78 | lowTemp_label = tk.Label( 79 | master=root, 80 | text=(f"The low will be {weather_Dict['lowTemp']} degrees"), 81 | padx=10, 82 | ) 83 | lowTemp_label.grid(row=3, column=0, columnspan=6, sticky=tk.W) 84 | 85 | highTemp_label = tk.Label( 86 | master=root, 87 | text=f"The high will be {weather_Dict['highTemp']} degrees", 88 | padx=10, 89 | ) 90 | highTemp_label.grid(row=4, column=0, columnspan=6, sticky=tk.W) 91 | 92 | currentTemp_label = tk.Label( 93 | master=root, 94 | text=f"The current temperature is {weather_Dict['currentTemp']} degrees", 95 | padx=10, 96 | ) 97 | currentTemp_label.grid(row=5, column=0, columnspan=6, sticky=tk.W) 98 | 99 | 100 | def get_weather(): 101 | city_name = city_text.get() 102 | 103 | populate_weather(city_name) 104 | 105 | 106 | # --------tkinter GUI--------- 107 | root = tk.Tk() 108 | 109 | root.title("Weather") 110 | root.geometry("500x200") 111 | 112 | city_text = tk.StringVar() 113 | city_label = tk.Label(root, text="City Name", font=("bold", 14), padx=10) 114 | city_label.grid(row=0, column=0) 115 | city_entry = tk.Entry(root, textvariable=city_text) 116 | city_entry.grid(row=0, column=1) 117 | 118 | weather_btn = tk.Button(root, text="Get Weather", width=12, command=get_weather) 119 | weather_btn.grid(row=0, column=2) 120 | 121 | root.mainloop() 122 | -------------------------------------------------------------------------------- /requiment.txt: -------------------------------------------------------------------------------- 1 | Requiment to run this app: 2 | -> python==3.7+ 3 | -> tkinter== 4 | 5 | --------------------------------------------------------------------------------