|
| 1 | +#pip install geopy |
| 2 | +#pip install requests |
| 3 | +#Enter your API KEY from AIR VISUAL API |
| 4 | + |
| 5 | +import tkinter as tk |
| 6 | +import requests |
| 7 | + |
| 8 | +# Create the main application window with a size of 400x400 |
| 9 | +app = tk.Tk() |
| 10 | +app.geometry("400x400") |
| 11 | +app.title("Live AQI") |
| 12 | +app.configure(bg="#F0F0F0") # Set the background color of the window |
| 13 | + |
| 14 | +# Create labels, entry fields, and a button for user input |
| 15 | +city_label = tk.Label(app, text="City:") |
| 16 | +city_label.pack() |
| 17 | +city_label.configure(bg="#F0F0F0") # Set the background color of the label |
| 18 | +city_entry = tk.Entry(app) |
| 19 | +city_entry.pack() |
| 20 | + |
| 21 | +state_label = tk.Label(app, text="State:") |
| 22 | +state_label.pack() |
| 23 | +state_label.configure(bg="#F0F0F0") # Set the background color of the label |
| 24 | +state_entry = tk.Entry(app) |
| 25 | +state_entry.pack() |
| 26 | + |
| 27 | +country_label = tk.Label(app, text="Country:") |
| 28 | +country_label.pack() |
| 29 | +country_label.configure(bg="#F0F0F0") # Set the background color of the label |
| 30 | +country_entry = tk.Entry(app) |
| 31 | +country_entry.pack() |
| 32 | + |
| 33 | +get_aqi_button = tk.Button(app, text="Get AQI", bg="#3498db") # Set the background color of the button |
| 34 | +get_aqi_button.place(x=180, y=200) |
| 35 | + |
| 36 | +# Add some space between the entry fields and the button |
| 37 | +spacer_label = tk.Label(app, text="", bg="#F0F0F0") |
| 38 | +spacer_label.place(x=90, y=150) |
| 39 | + |
| 40 | +# Create labels to display the AQI information |
| 41 | +city_label = tk.Label(app, text="City:") |
| 42 | +city_label.pack() |
| 43 | +city_label.configure(bg="#F0F0F0") # Set the background color of the label |
| 44 | +state_label = tk.Label(app, text="State:") |
| 45 | +state_label.pack() |
| 46 | +state_label.configure(bg="#F0F0F0") # Set the background color of the label |
| 47 | +aqi_label = tk.Label(app, text="AQI:") |
| 48 | +aqi_label.pack() |
| 49 | +aqi_label.configure(bg="#F0F0F0") # Set the background color of the label |
| 50 | + |
| 51 | +AIRVISUAL_API_KEY = "YOUR API KEY" |
| 52 | +AIRVISUAL_API_URL = "https://api.airvisual.com/v2/city" |
| 53 | + |
| 54 | +def get_aqi(): |
| 55 | + city = city_entry.get().strip() # Remove leading/trailing whitespaces |
| 56 | + state = state_entry.get().strip() |
| 57 | + country = country_entry.get() |
| 58 | + |
| 59 | + if city.lower() == "mumbai": |
| 60 | + city = "Mumbai" |
| 61 | + state = "Maharashtra" |
| 62 | + |
| 63 | + params = { |
| 64 | + "city": city, |
| 65 | + "state": state, |
| 66 | + "country": country, |
| 67 | + "key": AIRVISUAL_API_KEY, |
| 68 | + } |
| 69 | + response = requests.get(AIRVISUAL_API_URL, params=params) |
| 70 | + |
| 71 | + if response.status_code == 200: |
| 72 | + data = response.json() |
| 73 | + aqi = data["data"]["current"]["pollution"]["aqius"] |
| 74 | + city_label.config(text=f"City: {city}") |
| 75 | + state_label.config(text=f"State: {state}") |
| 76 | + aqi_label.config(text=f"AQI: {aqi}") |
| 77 | + else: |
| 78 | + aqi_label.config(text="Error: Unable to fetch AQI data") |
| 79 | + city_label.config(text=f"City: -") |
| 80 | + state_label.config(text=f"State: -") |
| 81 | + |
| 82 | + |
| 83 | +get_aqi_button.config(command=get_aqi) |
| 84 | + |
| 85 | +app.mainloop() |
0 commit comments