Weather App

clouds

05Weather App:

  1. First need to work on the GUI using tkinter:

This this I am gona use python classes. Setting up the root frame/ main window.

class App:
    def __init__(self, root):
        root.title("Weather App")

        width = 600
        height = 500
        screenwidth = root.winfo_screenwidth()
        screenheight = root.winfo_screenheight()
        alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        root.geometry(alignstr)
        root.resizable(width=False, height=False)
  1. Widgets inside the root :

Firstly creating the labels for the entries and then entries. Positioning the widgets using .place() this time:

city = tk.Label(root)
        ft = tk_font.Font(family='Helvetica Neue', size=15)
        city["font"] = ft
        city["fg"] = "white"
        city["justify"] = "center"
        city["text"] = "City"
        city.place(x=0, y=150, width=72, height=30)

        app_heading = tk.Label(root)
        ft = tk_font.Font(family='Helvetica Neue', size=30)
        app_heading["font"] = ft
        app_heading["fg"] = "white"
        app_heading["justify"] = "center"
        app_heading["text"] = "Weather App"
        app_heading.place(x=100, y=0, width=383, height=128)

        province = tk.Label(root)
        ft = tk_font.Font(family='Helvetica Neue', size=15)
        province["font"] = ft
        province["fg"] = "white"
        province["justify"] = "center"
        province["text"] = "Province"
        province.place(x=0, y=200, width=70, height=25)

        country = tk.Label(root)
        ft = tk_font.Font(family='Helvetica Neue', size=15)
        country["font"] = ft
        country["fg"] = "white"
        country["justify"] = "center"
        country["text"] = "Country"
        country.place(x=0, y=250, width=70, height=25)

        self.city_name = tk.Entry(root)
        self.city_name["borderwidth"] = "1px"
        ft = tk_font.Font(family='Helvetica Neue', size=20)
        self.city_name["font"] = ft
        self.city_name["fg"] = "white"
        self.city_name["justify"] = "center"
        self.city_name["text"] = ""
        self.city_name.place(x=70, y=150, width=178, height=34)

        self.province_name = tk.Entry(root)
        self.province_name["borderwidth"] = "1px"
        ft = tk_font.Font(family='Helvetica Neue', size=20)
        self.province_name["font"] = ft
        self.province_name["fg"] = "white"
        self.province_name["justify"] = "center"
        self.province_name["text"] = ""
        self.province_name.place(x=70, y=200, width=178, height=39)

        self.country_name = tk.Entry(root)
        self.country_name["borderwidth"] = "1px"
        ft = tk_font.Font(family='Helvetica Neue', size=20)
        self.country_name["font"] = ft
        self.country_name["fg"] = "white"
        self.country_name["justify"] = "center"
        self.country_name["text"] = ""
        self.country_name.place(x=70, y=250, width=176, height=37)

creating buttons one to clear the inputs from the entry and reset the values and other to find the weather of the region according to the user input:

result_button = tk.Button(root)
        result_button["bg"] = "white"
        ft = tk_font.Font(family='Helvetica Neue', size=20)
        result_button["font"] = ft
        result_button["fg"] = "black"
        result_button["justify"] = "center"
        result_button["text"] = "Go"
        result_button.place(x=60, y=330, width=90, height=50)
        result_button["command"] = self.show_weather

        clear_button=tk.Button(root)
        clear_button["bg"] = "#efefef"
        ft = tk_font.Font(family='Times',size=20)
        clear_button["font"] = ft
        clear_button["fg"] = "#000000"
        clear_button["justify"] = "center"
        clear_button["text"] = "CLEAR"
        clear_button.place(x=150,y=330,width=90,height=50)
        clear_button["command"] = self.clear

Now, we need to put widgets that show the final result that will be the temp and weather and the city:

 weather_img = tk.Label(root)
        ft =tk_font.Font(family='Helvetica Neue', size=20)
        weather_img["font"] = ft
        weather_img["fg"] = "white"
        weather_img["justify"] = "center"
        weather_img["text"] = ""
        weather_img.place(x=380, y=130, width=141, height=93)

        self.main_city = tk.Label(root)
        ft = tk_font.Font(family='Helvetica Neue', size=20)
        self.main_city["font"] = ft
        self.main_city["fg"] = "white"
        self.main_city["justify"] = "center"
        self.main_city["text"] = ""
        self.main_city.place(x=380, y=360, width=142, height=30)

        self.temp = tk.Label(root)
        ft = tk_font.Font(family='Helvetica Neue', size=20)
        self.temp["font"] = ft
        self.temp["fg"] = "white"
        self.temp["justify"] = "center"
        self.temp["text"] = ""
        self.temp.place(x=380, y=270, width=100, height=78)

        self.weather = tk.Label(root)
        ft = tk_font.Font(family='Helvetica Neue', size=20)
        self.weather["font"] = ft
        self.weather["fg"] = "white"
        self.weather["justify"] = "center"
        self.weather["text"] = " "
        self.weather.place(x=410, y=240, width=100, height=25)
  1. Now, need to some global variables to store info:

lat = 0
long = 0
r_temp = 0
r_weather = 0

these variable will store the longitude and latitude of the region entered by the user using API’s.

  1. creating function to find the values for the above variable and display in the widgets we made for the result to display:
def show_weather(self):
        global r_temp
        global r_weather
        
        city_code= self.city_name.get()
        country_code = self.country_name.get()
        state = self.province_name.get()
        url="http://api.openweathermap.org/geo/1.0/direct?q="+city_code+","+state+","+country_code+"&limit=1&appid=#"
        result=requests.get(url).json()
        lat=str(result[0]["lat"])
        lon=str(result[0]["lon"])
        print(lat,lon)
        weather_url="https://api.openweathermap.org/data/2.5/weather?lat="+lat+"&lon="+lon+"&appid=#"
        weather_res=requests.get(weather_url).json()
        print(weather_res['weather'][0]['description'])
        
        # self.main_city.config(text = city_code)
        # temp.config(text = str(weather_res[1][1]))
        r_weather =  weather_res['weather'][0]['description']
        self.weather.config(text = r_weather )
        
        r_temp = weather_res['main']['temp']
        self.temp.config(text = r_temp )
        self.main_city.config(text = city_code)

        self.city_name.delete(0, 'end')
        self.province_name.delete(0, 'end')
        self.country_name.delete(0, 'end')
  1. function to clear up the data:
 def clear(self):
        self.weather.config(text = "" )
        self.temp.config(text = "" )
        self.main_city.config(text = "")
        self.city_name.delete(0, 'end')
        self.province_name.delete(0, 'end')
        self.country_name.delete(0, 'end')
  1. Final loop to run the app:
if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    root.mainloop()