WEATHER APP
USING PYTHON
AND TKINTER
A PROJECT FOR DIPLOMA SEMESTER 5
NAME – TIRTH J CHAUHAN
ENROLLMENT NO.:226040316007
INSTITUTE MENTOR – HIRAL.B.CHAUHAN
INTRODUCTION
Objective:
• To create a Weather App using Python's Tkinter library.
• The app fetches and displays current weather data for a specified city.
Key Libraries Used:
• Tkinter for GUIObjective:To create a Weather App using Python's Tkinter library.The app fetches and
displays current weather data for a specified city.Key Libraries Used:Tkinter for GUIGeopy for
geolocationTimezoneFinder and pytz for timezone handlingRequests for making API calls to fetch
weather data.
• Geopy for geolocation
• TimezoneFinder and pytz for timezone handling
• Requests for making API calls to fetch weather data
PREREQUISITES
SoftwareSoftware:Python 3.xRequired libraries: tkinter, geopy,
timezonefinder, requests, pytzAPI:OpenWeatherMap API for weather
data:
• Python 3.x
• Required libraries: tkinter, geopy, timezonefinder, requests, pytz
API:
• OpenWeatherMap API for weather data
PROJECT STRUCTURE
Main File: weather_app.py
Resources:
• Images: search.png, searchicon.png, weather.png, info_box.png
• API Key: OpenWeatherMap API key
GUI SETUP - TKINTER WINDOW
app = Tk()
app.title("Weather App")
app.geometry("900x500+300+200")
GUI Window
app.resizable(False, False)
Explanation:
• Creates the main application window.
• Sets the title, size, and disables window resizing.
WEATHER DATA RETRIEVAL
city = textfield.get()
geolocator = Nominatim(user_agent="geoapiExercises")
location = geolocator.geocode(city)
• Retrieves the city name from the user input.
• Uses Geopy to get the geographical coordinates of the city.
HANDLING TIMEZONES
obj = TimezoneFinder()
result = obj.timezone_at(lng=location.longitude, lat=location.latitude)
home = pytz.timezone(result)
local_time = datetime.now(home)
• TimezoneFinder finds the city's timezone.
• pytz handles the conversion to local time
FETCHING WEATHER DATA
api_key = "your_api_key"
api = f"http://api.openweathermap.org/data/2.5/weather?
lat={location.latitude}&lon={location.longitude}&appid={api_key}&uni
ts=metric"
json_data = requests.get(api).json()
• Constructs the API URL with the latitude and longitude.
• Sends a request to OpenWeatherMap API to fetch weather data
ERROR HANDLING
if json_data['cod'] != 200:
messagebox.showerror("API Error", "Failed to retrieve weather data.")
Explanation:
• Checks the API response for errors.
• Displays an error message if the API call fails.
DISPLAYING WEATHER DATA
t_label.config(text=f"{temp}°C")
c.config(text=f"{condition} | FEELS LIKE {temp}°C")
• Updates the GUI labels with the temperature, condition, and other
weather details
GUI ELEMENTS - SEARCH BAR
textfield = tk.Entry(app, justify="center", width=17, font=("poppins", 25, "bold"), bg="light
blue", border=0, fg="white")
Explanation:
• Creates the search bar for city input.
•Use the Updates the GUI labels with the temperature, condition, and other
weather detailsEntry widget from Tkinter to create a text box for user input.
•Set properties such as width, font, background color, and text color for
better aesthetics
GUI ELEMENTS - SEARCH ICON
Search_icon = PhotoImage(file="path_to_image")
myimage_icon = Button(image=Search_icon, borderwidth=0, cursor="hand2",
command=getweather)
Explanation:
Creates the search button that triggers the weather data retrieval.
• In the Weather App, the search button is a crucial component that triggers the retrieval of
weather data when clicked. This button is created using the Tkinter Button widget, which is
linked to the search_weather function. When the user enters a city name in the search bar
and clicks the button or presses the Enter key, the app captures the input and initiates a
process to fetch the corresponding weather information. The button's command is set to call
the search_weather function, which handles the geolocation lookup and API request to
OpenWeatherMap. This seamless integration allows users to easily access real-time weather
updates with a simple click, enhancing the overall user experience of the app.
GUI ELEMENTS - WEATHER LOGO AND
FRAME
Logo_image = PhotoImage(file="path_to_image")
logo = Label(image=Logo_image)
Explanation:
Adds a logo and a frame to the application window.
• To enhance the visual appeal of the Weather App, a logo and a frame are added to
the application window. The logo is incorporated using the Tkinter Label widget,
which displays the weather logo image at a designated position within the interface,
providing branding and context for the app. Additionally, a frame is created using
the Frame widget, which serves as a structured area to group related elements and
improve layout organization. This frame can be styled with a background color and
border, helping to visually separate different sections of the app, such as the
weather information display and the input area, thereby creating a more polished
and user-friendly interface.
GUI ELEMENTS - LABELS
label1 = Label(app, text="WIND", font=("Helvetica", 15, "bold"),
fg="white", bg="#1ab5ef")
Explanation:
• Creates labels for displaying weather details like wind,
humidity,desctiption,pressure etc
FINAL GUI DISPLAY
Content:
• A screenshot of the final application interface.
Brief explanation of how the app works:
• The Weather App operates by allowing users to input the name of a city to retrieve
real-time weather information. Upon launching the app, users enter a city name in
the search bar and can either click the search button or press the Enter key to
initiate the weather retrieval process. The app utilizes the geopy library to convert
the city name into geographic coordinates, which are then used to make an API call
to OpenWeatherMap. This call fetches current weather data, including temperature,
humidity, wind speed, and weather conditions. The app dynamically updates the
user interface to display this information, ensuring users receive immediate
feedback. Additionally, it incorporates error handling to manage issues such as
invalid city names or API errors, providing user-friendly notifications when problems
arise. Overall, the app aims to deliver accurate and timely weather updates,
enhancing users' ability to plan their daily activities.
ERROR HANDLING AND DEBUGGING
Content:
• Common errors and how to handle them.
• Implementing robust error handling improves user experience and ensures the weather app
remains functional even when unexpected issues arise. Feel free to modify the content to fit
your presentation style! If you need further assistance or adjustments
• Examples of try-except blocks
• In the weather app, try-except blocks are used to handle potential errors gracefully,
ensuring the application remains responsive and user-friendly. For example, when retrieving
weather data from the OpenWeatherMap API, a try-except block can catch exceptions
related to network issues or invalid responses.
CHALLENGES FACED
Content: Implementing robust error handling to manage different
types of API errors (e.g., 403 Forbidden, 404 Not Found).
• Briefly describe any challenges or issues encountered
during development. Ensuring user-friendly error messages for invalid inputs.
1. API Integration: 4. User Interface Design:
Difficulty in understanding the OpenWeatherMap API Challenges in creating an intuitive and visually
documentation. appealing GUI using Tkinter.
Issues with obtaining and using the API key correctly. Balancing aesthetics with functionality to enhance user
experience.
2. Geocoding Accuracy:
5. Testing and Validation:
Inaccurate geocoding results for certain city names,
leading to "city not found" errors. Difficulty in testing edge cases, such as network failures
or incorrect city names.
Handling variations in city names and spellings.
Ensuring compatibility across different devices and
3. Error Handling:
screen sizes.
CONCLUSION
Content:
Summary of the project.
• The Weather App serves as a practical tool for users to quickly access weather
information for any city, showcasing the integration of various Python libraries
and the principles of GUI development.
Key learnings and skills gained
• The development of the Weather App provided a comprehensive learning
experience, combining technical skills in programming, API usage, and GUI
design with practical problem-solving and project management abilities.
THANK YOU