Skip to content

Commit b5f1bfb

Browse files
authored
Merge pull request #759 from mgndolan/Tkinter_Python_GUI
Tkinter Python GUI #696 - Weather Forecaster
2 parents 123b4b0 + d65bb4a commit b5f1bfb

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

tkinter_python_gui/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Tkinter Python GUI Example - US City Weather Forecaster
2+
3+
Tkinter is a Python module used to create cross-platform GUIs. USWeatherForecast.py demonstrates an example of a Tkinter application.
4+
5+
## How it works:
6+
1. Run the script `USWeatherForecast.py` by typing either:
7+
```bash
8+
python3 USWeatherForecast.py
9+
```
10+
or
11+
```bash
12+
python USWeatherForecast.py
13+
```
14+
2. A window will pop up that looks like the following screen:
15+
16+
![Windows 10 display of US Weather Forecaster application](./images/demo.png)
17+
18+
From this screen, enter in a US city name and click the "Get Weather" button.
19+
20+
3. The interface should update to display a table with the city's weather information.
21+
22+
![Windows 10 display of US Weather Forecaster with city entries](./images/demoData.png)
23+
24+
4. To add another city, enter a new city in the text box and click the "Add New City" button.
25+
26+
27+
## API Information
28+
The application displays the following information:
29+
30+
- The US city name entered by the user
31+
- The current temperature in Fahrenheit
32+
- The minimum daily temperature (Fahrenheit)
33+
- The maximum daily temperature (Fahrenheit)
34+
- The current weather condition
35+
- A brief description of the type of weather condition (more detail)
36+
37+
All of the above information is from the free, open-source [Open Weather Map RapidAPI](https://rapidapi.com/community/api/open-weather-map/)
38+
39+
## Author(s)
40+
41+
Megan Dolan @mgndolan
+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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()

tkinter_python_gui/images/demo.png

6.78 KB
Loading
20.8 KB
Loading

tkinter_python_gui/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tkinter
2+
requests

0 commit comments

Comments
 (0)