Skip to main content

Featured

Solving Economic Crisis Without Work-From-Home: A Systems Approach to Resource Prioritization

  1. The Economic Problem: Diagnosing the Crisis Type 1.1 Crisis Typology and Sector Dynamics Currency crises typically emerge from one or more of these imbalances: Current account deficits — Imports exceed exports; forex drains to cover the gap Capital account withdrawal — Foreign investors exit; hot money leaves Inflation-driven overvaluation — Real exchange rate strengthens despite nominal devaluation Debt servicing burden — External debt payouts drain reserves faster than exports can cover The empirical record shows that currency crises are sectoral crises —not aggregate demand crises. When Argentina devalued 75% in 2001, the economy contracted 10.9%, but manufacturing capacity utilization recovered within 18 months because input costs fell (Hausmann & Velasco, 2002). When Vietnam reformed in 1986, manufacturing capacity expansion drove recovery before demand-side effects materialized. Critical insight: Resource reallocation works when the constraint is supply-sid...

Building an Air Quality Index Notifier with Python and Tkinter

In this blog post, we'll walk through a simple Python application that notifies you about the Air Quality Index (AQI) of a specific location. Using Tkinter for the graphical interface, we'll fetch real-time AQI data from the Weatherbit API, display it on the GUI, and even have the application speak out the air quality status.


Understanding the Code:

1. Imports:

   - The code begins by importing necessary libraries like `requests` for API calls, `Tkinter` for the GUI, `gTTS` for text-to-speech, and `playsound` for playing audio.


2. Class `AQIApp`:

   - The class sets up the Tkinter window, labels, and buttons. The constructor initializes the GUI elements.

   - The `check_aqi` method fetches AQI data from Weatherbit API, updates the GUI, and speaks the air quality status based on predefined thresholds.

   - The `speak` method uses gTTS to convert text to speech and `playsound` to play the audio.

   - The `exit_app` method closes the application window.


3. Main Block:

   - Creates the main Tkinter window and an instance of `AQIApp`.

   - The application starts, fetching and displaying AQI information, and updates it every minute.


Building a simple AQI notifier using Python and Tkinter demonstrates the ease of creating desktop applications. This project not only provides a practical tool for checking air quality but also serves as a starting point for learning about APIs, GUI development, and integrating text-to-speech functionality.

import requests

from tkinter import Tk, Label, Button
from gtts import gTTS
from playsound import playsound

class AQIApp:
    def __init__(self, master):
        self.master = master
        master.title("AQI Notifier")

        self.label = Label(master, text="Air Quality Index:")
        self.label.pack()

        self.aqi_label = Label(master, text="")
        self.aqi_label.pack()

        self.check_aqi_button = Button(master, text="Check AQI", command=self.check_aqi)
        self.check_aqi_button.pack()

        self.exit_button = Button(master, text="Exit", command=self.exit_app)
        self.exit_button.pack()

        self.update_aqi()

    def check_aqi(self):
        api_key = 'your api key'
        city = 'Gurugram'
        state = 'Haryana'
        country = 'India'
        api_url = f'https://api.weatherbit.io/v2.0/current/airquality?city={city}&state={state}&country={country}&key={api_key}'

        try:
            response = requests.get(api_url)
            data = response.json()
           
            aqi = data['data'][0]['aqi']
            self.aqi_label.config(text=f"AQI: {aqi}")

            if 0 <= aqi <= 50:
                self.speak("The air quality is good and healthy.")
            elif 51 <= aqi <= 100:
                self.speak("The air quality is moderate.")
            elif 101 <= aqi <= 150:
                self.speak("The air quality is unhealthy for sensitive groups.")
            elif 151 <= aqi <= 200:
                self.speak("The air quality is unhealthy.")
            elif 201 <= aqi <= 300:
                self.speak("The air quality is very unhealthy.")
            elif aqi > 300:
                self.speak("The air quality is hazardous.")
        except Exception as e:
            print(f"Error fetching AQI: {e}")

        # Schedule the next update after 60000 milliseconds (1 minute)
        self.master.after(60000, self.update_aqi)

    def speak(self, text):
        tts = gTTS(text=text, lang='en')
        tts.save("output.mp3")
        playsound("output.mp3")

    def exit_app(self):
        self.master.destroy()

    def update_aqi(self):
        self.check_aqi()

if __name__ == "__main__":
    root = Tk()
    app = AQIApp(root)
    root.mainloop()

Comments

Popular Posts