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