Weather App in Python 2.0
Weather App in Python 2.0
In this tutorial, you will learn about how to create a GUI Weather App
in Python. It uses Open Weather Map API to fetch the latest weather
information of cities and places around the globe. Also, we will be
implementing the weather app with GUI (Graphical User Interface)
rather than the traditional boring ways, which are widely available,
showing output in CLI(Command Line Interface).
As a next step, we initialize our GUI window using the Tkinter module.
win = Tk()
In our code, we will be using Open Weather API (free tier) to get the
current weather information which is accurate and latest.
Here, comes the part where we add functionality to our code. This part
is the most crucial in getting correct weather information, as this
involves fetching data from the API and displaying it in an accurate
format.
To start with, we code the text field for the City Name we want the
weather for, along with the label to indicate so:
We use the Label method to generate a label of text to indicate the
purpose of the input field for city name.
Entry method is used to make an entry field for input of city name, to
check its weather.
The textvaraible widget is used to store the inputted value, in the
variable named: city_value
Other than these widgets we have also applied some styling to our code,
by font size, color, etc.
name_label = Label ( win,text="Back Bancher Weather App",
font=("Time New Roman",25,"bold"))
name_label.place(x=25,y=50,height=50, width=450)
city_name = StringVar()
list_name = ["Andhra Pradesh","Arunachal Pradesh ","Assam","Bihar","Chhattisgarh","Goa","Gujarat"
Kashmir","Jharkhand","Karnataka","Kerala","Madhya
Pradesh","Maharashtra","Manipur","Meghalaya","Mizoram","Nagaland","Odisha","Punjab","Rajasthan","
Pradesh","Uttarakhand","West Bengal","Andaman and Nicobar Islands","Chandigarh","Dadra and Nagar
Capital Territory of Delhi","Puducherry"]
com = ttk.Combobox(win,text="Back Bancher Weather App",values=list_name,
font=("Time New Roman",15,"bold"),textvariable=city_name)
com.place(x=25,y=120,height=50, width=450)
We code a Check Weather Button, on which we click to check the
weather of the user inputted city:
We give our button some styling, along with the name – ‘Check
Weather’. We use the ‘command‘ widget, which shows what function
(here, showWeather function) would run on the click (key press) of
the button, as coded in the previous step.
After adding this, we add the output elements in our code. The
elements on which our Weather information would be displayed.
Yet again, we add a label to title our result in the following text box
To display the output we use a text field, which gets its value, every
time the “Check Weather” button is pressed. This envokes the
function to check weather info fetched from the API after processing,
[output from the showWeather function]
On execution of our code, the Tkinter displays this as output:
def data_get():
city = city_name.get()
data = requests.get("https://api.openweathermap.org/data/2.5/weather?q="+city+"&appid=3c91f2ad78cc
w_label1.config(text=data["weather"][0]["main"])
wb_label1.config(text=data["weather"][0]["description"])
temp_label1.config(text=str(data["main"]["temp"]-273.15))
pre_label1.config(text=data["main"]["pressure"])
win = Tk()
done_button = Button(win,text="Done",
font=("Time New Roman",15,"bold"),command=data_get)
done_button.place(y=190,height=50,width=100,x=200)
win.mainloop()