-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdays_until.py
executable file
·276 lines (213 loc) · 8.01 KB
/
days_until.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
"""
Pick a date in the future and calculate how much time there is left until said date.
You can save and delete entries. Those entries will have the option of being updated
by the minute when the program is running.
Create the ability to set reminders and alerts when there are x days left for a given entry.
"""
import tkinter as tk
from tkinter import ttk
import ttkthemes
import csv
import json
from tkcalendar import Calendar, DateEntry
import datetime
import random
from functools import partial
import subprocess
import os
future = None
# dates_file = 'none.csv'
# dates_file = 'mine.csv'
script_path = os.path.dirname(os.path.abspath(__file__))
dates_file = 'dates.csv'
dates_file = os.path.join(script_path, dates_file)
reminders_file_name = 'reminders.json'
reminders_file_path = os.path.join(script_path, reminders_file_name)
def calc_days():
global future
today = datetime.date.today()
# get_date() method returns the date as a datetime object
future = date_picker.get_date()
print(future)
days_left = future - today
event_name = cal_event.get()
days_label = tk.Label(root, text=days_left)
days_label.pack()
if not event_name or event_name == placeholder_text:
event_name = future
text = f"{days_left} days until {event_name}"
days_label.config(text=text)
def save_date():
global future
global dates_dict
event_name = cal_event.get()
print(future)
if not event_name or event_name == placeholder_text:
event_name = random.randint(0,100)
today = datetime.date.today()
# don't let falsy values be stored
if future:
dates_dict[event_name] = str(future)
with open(dates_file, 'w') as f:
writer = csv.writer(f)
writer.writerow(dates_dict.keys())
writer.writerow(dates_dict.values())
update_dates(dates_dict)
def load_dates():
try:
with open(dates_file, 'r') as csvfile:
csvreader = csv.DictReader(csvfile)
row = {} # fallback val in case file is empty
for row in csvreader:
pass
return row
except FileNotFoundError as e:
with open(dates_file, 'x') as file:
pass
return {} # empty dictionary for dates_dict var
def update_dates(dates_dict):
# loop through the widgets in the parent widget and destroy them, then put the updated dates.
for widget in entries_frame.winfo_children():
widget.destroy()
print_dates(dates_dict)
def delete_entry(entry_key):
global dates_dict
dates_dict.pop(entry_key)
with open(dates_file, 'w') as f:
writer = csv.writer(f)
writer.writerow(dates_dict.keys())
writer.writerow(dates_dict.values())
# print_dates()
update_dates(dates_dict)
def order_by():
global dates_dict
global current_order
# flip through the different orderings
# 'none' is the order the entries were added.
if current_order == 'none':
current_order = 'desc'
rev = False
elif current_order == 'desc':
current_order = 'asc'
rev = True
elif current_order == 'asc':
current_order = 'none'
update_dates(dates_dict)
return
today = datetime.date.today()
def str_to_days(item: tuple):
v = item[1]
date = datetime.datetime.strptime(v, "%Y-%m-%d").date()
days_left = date - today
days_left = days_left.days
return days_left
# new_dict['k'] = days_left
sorted_dict = dict(sorted(dates_dict.items(), key=str_to_days, reverse=rev))
update_dates(sorted_dict)
# there is some repetition here that needs to be eliminated.
def add_reminder(key, spinvar):
# remind again when there is 'new_reminder' days left
print(spinvar.get())
new_reminder = int(spinvar.get())
try:
with open(reminders_file_path, 'r') as reminders:
rem = reminders.read()
if not rem:
rem = {}
else:
rem = json.loads(rem)
if key not in rem:
rem[key] = {}
rem[key]['date'] = dates_dict[key]
rem[key]['reminders'] = new_reminder
except FileNotFoundError as e:
label = tk.Label(top_frm, text=e)
# label.pack()
rem = {}
rem[key] = {}
rem[key]['date'] = dates_dict[key]
rem[key]['reminders'] = new_reminder
with open(reminders_file_path, 'w') as reminders:
reminders_dump = json.dump(rem, reminders)
def print_dates(dates_dict):
# global dates_dict
today = datetime.date.today()
if not dates_dict:
return
for k, v in dates_dict.items():
future = datetime.datetime.strptime(v, "%Y-%m-%d").date()
days_left = future - today # return a datetime.timedelta object => 12 days, 0:00:00
days_left = days_left.days
text = f"days until {k}, {future}"
# text = f"{days_left} days until {k}, {future}"
entry_frame = ttk.Frame(entries_frame, padding=4)
days_label = ttk.Label(entry_frame, text=days_left, foreground='blue')
label = ttk.Label(entry_frame, text=text)
del_btn = ttk.Button(entry_frame, text=f'delete', command=partial(delete_entry, k))
# spinbox for selecting when to remind
# assoaciate with a StringVar to pass that value to the partial function so that the spinbox is assocaited with the button
spinbox_var = tk.StringVar()
spinbox = ttk.Spinbox(entry_frame, from_=1, to=20, width=4, textvariable=spinbox_var)
add_reminder_btn = ttk.Button(entry_frame, text='add reminder', command=partial(add_reminder, k, spinbox_var))
days_label.pack(side='left')
label.pack(anchor=tk.W, side='left', padx=8)
del_btn.pack(side='left', padx=5)
spinbox.pack(side='left', padx=3)
add_reminder_btn.pack(side='left')
entry_frame.pack()
def notify_func():
# notify2 needs dbus, so you need to be running a desktop environment like GNOME or KDE
# notify2.init("tkinter app")
# notification = notify2.Notification('HEllo!', 'this is a message')
# notification.show()
title = 'hi'
message = 'hello world'
subprocess.run(['notify-send', title, message])
# functions for entry placeholder
def on_entry_focus_in(event):
if cal_event.get() == placeholder_text:
cal_event.delete(0, tk.END)
cal_event.configure(show='')
cal_event.configure(foreground='black')
def on_entry_focus_out(event):
if cal_event.get() == '':
cal_event.insert(0, placeholder_text)
cal_event.configure(foreground='gray')
root = tk.Tk()
root.title("Days Until")
style = ttkthemes.ThemedStyle(root)
style.set_theme('plastik')
top_frm = ttk.Frame(root, padding=15)
top_frm.pack()
current_order = 'none'
header = ttk.Label(top_frm, text="Days Until", font=(18))
header.pack(pady=8)
load_dates_button = ttk.Button(top_frm, text="load", command=print_dates)
# load_dates_button.pack()
date_input_frm = ttk.Frame(top_frm, padding=8)
cal_event = ttk.Entry(date_input_frm)
cal_event.pack(side='left', padx=10)
placeholder_text = "Enter event name"
cal_event.insert(0, placeholder_text)
cal_event.configure(foreground='gray')
cal_event.bind("<FocusIn>", on_entry_focus_in)
cal_event.bind("<FocusOut>", on_entry_focus_out)
date_picker = DateEntry(date_input_frm)
date_picker.pack(side='right')
date_input_frm.pack()
calc_days_btn = ttk.Button(top_frm, text="calculate days", command=calc_days)
calc_days_btn.pack(pady=8)
entries_frame = ttk.Frame(top_frm, padding=5)
entries_frame.pack()
dates_dict = load_dates() # load dates automatically
print_dates(dates_dict)
notify_btn = ttk.Button(top_frm, text='notification', command=notify_func)
# notify_btn.pack()
order_button = ttk.Button(top_frm, text='sort', command=order_by)
order_button.pack(pady=3)
save_button = ttk.Button(top_frm, text='save Date', command=save_date)
quit_button = ttk.Button(top_frm, text='Quit', command=root.destroy)
quit_button.pack(side='bottom', pady=3)
save_button.pack(side='bottom', pady=3)
root.mainloop()