-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
233 lines (168 loc) · 6.47 KB
/
app.py
File metadata and controls
233 lines (168 loc) · 6.47 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
from tkinter import *
from tkinter import messagebox
from plyer import notification
import requests
import sys
import os
import json
from pystray import Icon, MenuItem, Menu
from PIL import Image
import threading
from threading import Thread
import webbrowser
# API KEY
api_key = 'YOUR KEY' # <----------- Change this
# Window
window = Tk()
window['bg'] = '#6e91a1'
window.title('WeatherPulse')
window.geometry("300x385")
window.resizable(width=False, height=False)
# Icon
if hasattr(sys, '_MEIPASS'):
icon_path = os.path.join(sys._MEIPASS, 'appicon.ico')
else:
icon_path = "C:\\............\\WeatherPulse\\icon.ico" # <----- Write yout path!
window.iconbitmap(icon_path)
# -------- Settings -------- #
# Settings path and import
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(sys.argv[0])))
settings_path = os.path.join(base_path, 'settings.json')
with open(settings_path, 'r') as file:
app_settings = json.load(file)
# Variables
city = app_settings['city']
measurement = StringVar(value=app_settings['temp_unit'])
# Settings save function
def save():
if not ent_city.get().strip():
messagebox.showerror("Error!", "Please enter the city!")
return
app_settings['city'] = ent_city.get()
app_settings['temp_unit'] = measurement.get()
try:
with open(settings_path, 'w') as file:
json.dump(app_settings, file, indent=4)
print("Settings saved.")
except Exception as e:
print(f"Saving error: {e}")
global city
city = app_settings['city']
request()
# -------- Functions -------- #
### Notification ###
def send_notification(description, temp, humidity):
if measurement.get() == 'fahrenheit':
symbol = '°F'
else:
symbol = '°C'
notification.notify(
title="Weather!",
message=f'It is {description} in {city} right now, temperature {temp}{symbol}. Humidity is {humidity}%.',
timeout=10
)
### Request ###
def request():
if not city:
messagebox.showerror("Error!", "Please enter the city and save settings!")
return
else:
if measurement.get() == 'fahrenheit':
url= f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=imperial'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
temp_rn = data['main']['temp']
description_rn = data['weather'][0]['description']
humidity_rn = data['main']['humidity']
final_text = f'{city}\n======================\nTemp: {temp_rn} °F\nConditions: {description_rn}\nHumidity: {humidity_rn}%'
output.config(state=NORMAL)
output.delete("1.0", END)
output.insert(END, final_text)
output.config(state=DISABLED)
send_notification(description_rn, temp_rn, humidity_rn)
else:
url= f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
temp_rn = data['main']['temp']
description_rn = data['weather'][0]['description']
humidity_rn = data['main']['humidity']
final_text = f'{city}\n======================\nTemp: {temp_rn} °C\nConditions: {description_rn}\nHumidity: {humidity_rn}%'
output.config(state=NORMAL)
output.delete("1.0", END)
output.insert(END, final_text)
output.config(state=DISABLED)
send_notification(description_rn, temp_rn, humidity_rn)
window.after(3600000, request)
# -------- UI -------- #
### Main ###
main = Frame(window)
main.pack(fill=BOTH, expand=True)
# Output field
output = Text(main, wrap=WORD,
font=("Microsoft New Tai Lue", 12))
output.place(relx=0.5, y=15, anchor='n', width=250, height=150)
output.config(state=DISABLED)
# CBK button
def github():
webbrowser.open("https://github.com/pythonCBK/weatherpulse/")
btn_cbk = Button(main, text="by CBK",
relief='flat', highlightthickness=0, borderwidth=0,
font=("Microsoft New Tai Lue", 8, "underline"), command=github)
btn_cbk.place(relx=0.5, y=365, anchor='n')
### Settings ###
# Text <Settings>
txt_city = Label(main, text="Settings",
font=("Microsoft New Tai Lue", 14, "bold"))
txt_city.place(relx=0.5, y=180, anchor='n')
# Txt <Enter the city>
txt_city = Label(main, text="Enter the required city:",
font=("Microsoft New Tai Lue", 12))
txt_city.place(relx=0.5, y=215, anchor='n')
# City entry
ent_city = Entry(main, font=("Microsoft New Tai Lue", 11))
ent_city.place(relx=0.5, y=240, anchor='n')
ent_city.insert(0, city)
# Txt <Select the measurement>
txt_select = Label(main, text="Select measurement:",
font=("Microsoft New Tai Lue", 12))
txt_select.place(relx=0.5, y=275, anchor='n')
# Celsius
celsius = Radiobutton(main, text="Celsius",
font=("Microsoft New Tai Lue", 11), value="celsius", variable=measurement)
celsius.place(relx=0.32, y=300, anchor='n')
# Fahrenheit
fahrenheit = Radiobutton(main, text="Fahrenheit",
font=("Microsoft New Tai Lue", 11), value="fahrenheit",variable=measurement)
fahrenheit.place(relx=0.68, y=300, anchor='n')
# Save button
btn_save = Button(main, text="Save changes",
font=("Microsoft New Tai Lue", 12), command=save)
btn_save.place(relx=0.5, y=335, anchor='n', height=25)
# -------- Tray -------- #
# Icon
def tray_icon():
icon = Icon("WeatherPulse")
icon.icon = Image.open(icon_path)
icon.menu = Menu(MenuItem("Quit", on_quit))
icon.run()
# Quit
def on_quit(icon, item):
icon.stop()
window.quit()
# To tray
def to_tray():
window.withdraw()
image = Image.open(icon_path)
icon = Icon("WeatherPulse", image, menu=Menu(MenuItem("Open", restore_window), MenuItem("Exit", on_quit)))
threading.Thread(target=icon.run).start()
# Restore window
def restore_window(icon, item):
window.deiconify()
icon.stop()
### On start up ###
request()
window.protocol("WM_DELETE_WINDOW", to_tray)
window.mainloop()