-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
109 lines (82 loc) · 2.99 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Import subprocess module
import subprocess
# import all other modules
import tkinter as tk
import python_weather
import asyncio
import os
import geopy
# Get the requirements.txt file path
requirements_file_path = "requirements.txt"
# Install all requirements from the requirements.txt file
subprocess.run(["pip", "install", "-r", requirements_file_path])
root = tk.Tk()
root.title("Thermometer App")
root.minsize(300, 200)
root.config(bg="black")
def validate_city(city):
if not city:
return False
geolocator = geopy.Nominatim(user_agent="temperature-app") # initialize geopy geolocator
try:
location = geolocator.geocode(city) # get location from city name
if location: # if location is not None
return True
else:
return False
except ValueError("City not recognised"): # handle geopy exceptions
return False
def separator():
sep = tk.Frame(root, height=2, bg="black")
sep.pack(fill="x", expand=True)
label = tk.Label(root, text="Please Enter Your City: ", font="calibri", bg="black", fg="white")
entry = tk.Entry(root, font="calibri", bg="black", fg="white", highlightbackground="white", highlightcolor="white")
entry.insert(0, "")
label.pack()
for i in range(6):
separator()
entry.pack()
for i in range(6):
separator()
async def get_weather(city):
async with python_weather.Client(unit=python_weather.METRIC) as client:
weather = await client.get(city)
weather = weather.current.temperature
return weather
def submit_entry():
city = entry.get()
if validate_city(city):
weather = asyncio.run(get_weather(city))
scale.set(weather)
scale_color(weather)
else:
invalid_city = tk.Label(root,
text="City is unrecognised, please format it as: City, State if from US, else format it" +
" as City, Country", bg="black", fg="white", wraplength=250)
invalid_city.pack()
invalid_city.after(5000, invalid_city.destroy)
submit = tk.Button(root, text="Submit", relief="raised", font="calibri", command=submit_entry)
submit.pack()
for i in range(6):
separator()
def scale_color(weather):
if int(weather) <= 0:
scale.config(bg="lightblue1", fg="black")
elif 0 < int(weather) <= 11:
scale.config(bg="blue", fg="white")
elif 11 < int(weather) <= 15:
scale.config(bg="yellow", fg="black")
elif 15 < int(weather) <= 21:
scale.config(bg="orange", fg="black")
else:
scale.config(bg="red", fg="white")
return weather
scale = tk.Scale(root, length=250, from_=50, to=-40, tickinterval=15,
orient="vertical")
scale.pack()
for i in range(6):
separator()
if __name__ == '__main__':
if os.name == 'nt':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
root.mainloop()