|
| 1 | +import tkinter as tk |
| 2 | +import tkinter.ttk as ttk |
| 3 | +import requests |
| 4 | + |
| 5 | +# Free Open Weather API (https://rapidapi.com/community/api/open-weather-map/) |
| 6 | +url = "https://community-open-weather-map.p.rapidapi.com/forecast" |
| 7 | + |
| 8 | +headers = {'x-rapidapi-host': "community-open-weather-map.p.rapidapi.com", |
| 9 | + 'x-rapidapi-key': |
| 10 | + "8e9e50a21bmshda23fe83770e19ep14a949jsn482dfa5dc300"} |
| 11 | + |
| 12 | +# initialize tkinter GUI |
| 13 | +root = tk.Tk() |
| 14 | +weather_frame = tk.Frame(root) |
| 15 | +weather_frame.grid(columnspan=3, row=3) |
| 16 | +weather_view = ttk.Treeview(weather_frame) |
| 17 | + |
| 18 | + |
| 19 | +# Initialize table for US city weather information |
| 20 | +def createTable(data, city): |
| 21 | + weather_view['columns'] = ('city', 'temperature', |
| 22 | + 'minTemp', 'maxTemp', |
| 23 | + 'condition', 'description') |
| 24 | + weather_view.column("#0", width=0, stretch=tk.NO) |
| 25 | + weather_view.column("city", anchor=tk.CENTER, width=85) |
| 26 | + weather_view.column("temperature", anchor=tk.CENTER, width=95) |
| 27 | + weather_view.column("minTemp", anchor=tk.CENTER, width=75) |
| 28 | + weather_view.column("maxTemp", anchor=tk.CENTER, width=75) |
| 29 | + weather_view.column("condition", anchor=tk.CENTER, width=75) |
| 30 | + weather_view.column("description", anchor=tk.CENTER, width=95) |
| 31 | + |
| 32 | + weather_view.heading("#0", text="", anchor=tk.CENTER) |
| 33 | + weather_view.heading("city", text="City Name", anchor=tk.CENTER) |
| 34 | + weather_view.heading("temperature", text="Temperature (F)", |
| 35 | + anchor=tk.CENTER) |
| 36 | + weather_view.heading("minTemp", text="Min Temp", anchor=tk.CENTER) |
| 37 | + weather_view.heading("maxTemp", text="Max Temp", anchor=tk.CENTER) |
| 38 | + weather_view.heading("condition", text="Condition", anchor=tk.CENTER) |
| 39 | + weather_view.heading("description", text="Description", anchor=tk.CENTER) |
| 40 | + NewEntry(data, city) |
| 41 | + |
| 42 | + |
| 43 | +# Populate table with weather information for each new US city |
| 44 | +def NewEntry(data, city): |
| 45 | + try: |
| 46 | + temp = str(round((((int(data['list'][1]['main']['temp']) |
| 47 | + - 273.15) * 1.8) + 32), 2)) |
| 48 | + temp_min = str(round((((int(data['list'][1]['main']['temp_min']) |
| 49 | + - 273.15) * 1.8) + 32), 2)) |
| 50 | + temp_max = str(round((((int(data['list'][1]['main']['temp_max']) |
| 51 | + - 273.15) * 1.8) + 32), 2)) |
| 52 | + weather = data['list'][2]['weather'][0]['main'] |
| 53 | + weather_desc = data['list'][2]['weather'][0]['description'] |
| 54 | + weather_view.insert(parent='', index='end', iid=None, text='', |
| 55 | + values=(city, temp, temp_min, temp_max, |
| 56 | + weather, weather_desc)) |
| 57 | + weather_view.grid(columnspan=3, row=3) |
| 58 | + except Exception: |
| 59 | + weather_view.insert(parent='', index='end', iid=None, text='', |
| 60 | + values=(city, None, None, None, None, None)) |
| 61 | + weather_view.grid(columnspan=3, row=3) |
| 62 | + |
| 63 | + |
| 64 | +root.title("US Weather Tracker") |
| 65 | + |
| 66 | +canvas = tk.Canvas(root, width=600, height=120) |
| 67 | +canvas.grid(columnspan=3, rowspan=5) |
| 68 | + |
| 69 | +# Weather Tracker application - title |
| 70 | +label = tk.Label(root, text="US Weather Tracker") |
| 71 | +label.grid(columnspan=3, row=0, pady=30) |
| 72 | +label.configure(font=("Courier", 25, "bold"), fg='#092653') |
| 73 | + |
| 74 | +# Weather Tracker application - title |
| 75 | +desc = tk.Label(root, text="Provide a US city name below...") |
| 76 | +desc.grid(columnspan=3, row=1, pady=10) |
| 77 | +desc.configure(font=("Courier", 10)) |
| 78 | + |
| 79 | +# Prompts the user to enter a city |
| 80 | +cityLabel = tk.Label(text="City Name:") |
| 81 | +cityLabel.grid(column=0, row=2, pady=20) |
| 82 | +cityLabel.configure(font=("Courier", 16)) |
| 83 | + |
| 84 | +cityEntry = tk.Entry(root, width=50, borderwidth=2) |
| 85 | +cityEntry.grid(column=1, row=2, pady=20) |
| 86 | + |
| 87 | +# Global variables to update button text and check if table exists |
| 88 | +newTable = True |
| 89 | +button_text = tk.StringVar() |
| 90 | +button_text.set("Get Weather") |
| 91 | + |
| 92 | + |
| 93 | +# Function to add a new city to the list of weather forecasts |
| 94 | +def addWeather(): |
| 95 | + global newTable |
| 96 | + if newTable: |
| 97 | + city = cityEntry.get() |
| 98 | + location = city + ",us" |
| 99 | + querystring = {"q": location} |
| 100 | + response = requests.request("GET", url, headers=headers, |
| 101 | + params=querystring) |
| 102 | + createTable(response.json(), city) |
| 103 | + button_text.set("Add New City") |
| 104 | + newTable = False |
| 105 | + elif not newTable: |
| 106 | + city = cityEntry.get() |
| 107 | + location = city + ",us" |
| 108 | + querystring = {"q": location} |
| 109 | + response = requests.request("GET", url, headers=headers, |
| 110 | + params=querystring) |
| 111 | + NewEntry(response.json(), city) |
| 112 | + |
| 113 | + |
| 114 | +# Button to add cities to weather forecast table |
| 115 | +getCityWeather = tk.Button(root, textvariable=button_text, |
| 116 | + command=addWeather, width=30, |
| 117 | + font=("Courier", 15), |
| 118 | + bg='#092653', |
| 119 | + fg="white").grid(columnspan=3, row=4, pady=10) |
| 120 | + |
| 121 | + |
| 122 | +root.mainloop() |
0 commit comments