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