-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToDoTk.py
More file actions
471 lines (397 loc) · 18 KB
/
ToDoTk.py
File metadata and controls
471 lines (397 loc) · 18 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
'''
Small Utility to track ToDo Items using tkinter
https://github.com/matmuc/ToDoPy
Copyright (C) 2024 Matthias Vogt
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 3
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
'''
import json
import datetime
import sys, io
from os.path import exists
import tkinter
from tkinter import *
from tkinter import ttk
from tkinter.font import Font, nametofont
from tkcalendar import DateEntry
def createNewEmptyJson():
obj = [
{
'ID': 1,
'Title':'Test1',
'Description': 'Nur ein Test',
'Priority': 1,
'Date': '2024-07-01',
'DueDate': '',
'Status': "New",
'Category': 'Test',
'Progress': [],
'DoneDate': ''
}
]
fname = "ToDo.json"
newFile = io.open(fname, mode="w", encoding="utf-8")
json.dump(obj, newFile, sort_keys=False, indent=2 )
newFile.close()
return fname
class Window(Tk):
def __init__(self):
super().__init__()
self.title("myToDos")
self.minsize(600,400)
self.geometry('1600x600')
self['bg'] = '#AC99F2'
#default_font = nametofont("TkDefaultFont")
#default_font = nametofont("TkFixedFont")
default_font = Font(family='Verdana')
default_font.configure(size=10) #9 is default
self.option_add("*Font", default_font)
self.topFrame = Frame(self, width=850, height=60)
self.topFrame.pack(expand=False, fill='x')
self.text = Label(self.topFrame, text="Nothing will work unless you do.")
self.text['bg'] = '#99FF99'
self.text.grid(row=1, column=4)
#self.text.pack()
self.NewToDoButton = Button(self.topFrame, text="NewToDo", command= self.handle_NewToDoButton_press)
#self.NewToDoButton.bind("", self.handle_NewToDoButton_press)
self.NewToDoButton.grid(row=1, column=1)
#self.NewToDoButton.pack()
self.showDone = IntVar()
self.showDone.set(0)
self.DoneCB = Checkbutton(self.topFrame, text='DONE', variable=self.showDone, onvalue=1, offvalue=0, command=self.onDoneCB)
self.DoneCB.grid(row=1, column=2)
self.reloadFileButton = Button(self.topFrame, text="reload", command=reloadFile)
self.reloadFileButton.grid(row=1, column=5)
self.catSel = StringVar()
self.categoriesCB = ttk.Combobox(self.topFrame, textvariable=self.catSel)
self.categoriesCB.grid(row=1, column=3)
self.categoriesCB['values'] = categories
self.categoriesCB.set('ALL')
self.categoriesCB.bind("<<ComboboxSelected>>", self.OnCatCbSel)
self.tableFrame = Frame(self, width=850, height=500)
self.tableFrame.pack(expand=True, fill='both')
#self.tableFrame.grid(row=3, column=1, columnspan=9, sticky="nsew")
self.scroll = Scrollbar(self.tableFrame)
self.scroll.pack(side=RIGHT, fill=Y)
self.scroll = Scrollbar(self.tableFrame, orient='horizontal')
self.scroll.pack(side=BOTTOM, fill=X)
self.todoTable = tkinter.ttk.Treeview(self.tableFrame)
#self.todoTable.place(relx=0.01, rely=0.1, width=850, height=500)
self.scroll.config(command=self.todoTable.yview)
self.scroll.config(command=self.todoTable.xview)
self.todoTable['columns'] = ('ID', 'Category', 'Title', 'Description', 'Priority', 'Date', 'DueDate', 'Status', 'Done')
self.todoTable.column("#0", width=0, stretch=NO)
self.todoTable.column("ID", anchor=CENTER, width=15)
self.todoTable.column("Category", anchor=CENTER, width=50)
self.todoTable.column("Title", anchor=CENTER, width=250)
self.todoTable.column("Description", anchor=CENTER, width=500)
self.todoTable.column("Priority", anchor=CENTER, width=15)
self.todoTable.column("Date", anchor=CENTER, width=60)
self.todoTable.column("DueDate", anchor=CENTER, width=60)
self.todoTable.column("Status", anchor=CENTER, width=60)
self.todoTable.column("Done", anchor=CENTER, width=60)
self.todoTable.heading("#0", text="", anchor=CENTER)
self.todoTable.heading("ID", text="ID", anchor=CENTER)
self.todoTable.heading("Category", text="Category", anchor=CENTER)
self.todoTable.heading("Title", text="Title", anchor=CENTER)
self.todoTable.heading("Description", text="Description", anchor=CENTER)
self.todoTable.heading("Priority", text="Priority", anchor=CENTER)
self.todoTable.heading("Date", text="Date", anchor=CENTER)
self.todoTable.heading("DueDate", text="DueDate", anchor=CENTER)
self.todoTable.heading("Status", text="Status", anchor=CENTER)
self.todoTable.heading("Done", text="Done", anchor=CENTER)
self.todoTable.tag_configure('urgent', background='#f9f995')
self.todoTable.tag_configure('overdue', background='#f9c795')
#self.todoTable.pack()
self.todoTable.pack(expand=True, fill='both')
self.showItems()
self.todoTable.bind("<Double-1>", self.OnDoubleClick)
self.statusText = Label(self, text=toDoFile, height=1)
self.statusText['bg'] = '#CCC'
self.statusText.pack(expand=False, fill='x')
def OnDoubleClick(self,event):
#row = self.todoTable.selection()[0]
#print("you clicked on row", row)
item = self.todoTable.identify('item',event.x,event.y)
#print(self.todoTable.item(item))
rowItem = self.todoTable.item(item)
#print("you clicked on", self.todoTable.item(item, "text"))
#print(rowItem["values"][0])
print("you clicked on", rowItem["text"])
EditToDo(self, getToDoItem(rowItem["values"][0]))
def OnCatCbSel(self,event):
self.catSel.set(self.categoriesCB.get())
print("Filter: "+self.catSel.get())
self.reload()
def handle_NewToDoButton_press(self):
print("New")
newObj = {
'ID': getNewID(),
'Category':'',
'Title':'',
'Description': '',
'Priority': '',
'Date': datetime.date.today().strftime("%Y-%m-%d"),
'DueDate': '',
'Status': "New",
'DoneDate': '',
'Progress': []
}
ItemsObject.append(newObj)
mbox = EditToDo(self, newObj)
def showItems(self):
global ItemsObject
print("Filter: " + self.catSel.get())
for item in ItemsObject:
if item['Status'].lower() == "deleted":
itemDeleted = True
else:
itemDeleted = False
if item['Status'].lower() == "done" or item['Status'].lower() == "obsolet":
itemDone = True
else:
itemDone = False
if (self.showDone.get() == 1 and itemDone) or (self.showDone.get() == 0 and not itemDone) and not itemDeleted:
tag = ''
try:
itemdate = datetime.datetime.strptime(item['DueDate'], '%Y-%m-%d').date()
today = datetime.date.today()
deltaDays = (today - itemdate).days
if deltaDays > 0 and not itemDone:
tag = 'overdue'
elif deltaDays > -7 and not itemDone:
tag = 'urgent'
except:
None
if self.catSel.get() =='ALL' or self.catSel.get() == item['Category']: # or len(self.catSel.get())<2
self.todoTable.insert(parent='', index='end', iid=str(item['ID']) , text='ID'+str(item['ID']), tags=(tag,),
values=(item['ID'],item['Category'], item['Title'], item['Description'], item['Priority'], item['Date'], item['DueDate'], item['Status'], item['DoneDate']))
def onDoneCB(self):
print("onDoneCB: ", self.showDone.get())
self.reload()
def reload(self):
x = self.todoTable.get_children()
for item in x: ## Changing all children from root item
self.todoTable.delete(item)
self.showItems()
self.categoriesCB['values'] = categories
class EditToDo(object):
root = None
ToDoObj = None
frame = None
def __init__(self, root, ToDoObj):
global categories
self.root = root
self.top = Toplevel(root)
self.top.wm_title = "Edit ToDo"
self.ToDoObj = ToDoObj
frm = Frame(self.top, borderwidth=4, relief='ridge')
frm.pack(fill='both', expand=True)
self.frame = frm
hlabel = Label(frm, text="Edit \""+ToDoObj['Title']+"\"")
hlabel.grid(row=0, column=1, columnspan=5, sticky="w")
self.teTitle = StringVar()
self.teTitle.set(ToDoObj["Title"])
Label(frm, text="Title").grid(row=2, column=1, sticky="e")
Entry(frm, width=50, textvariable=self.teTitle).grid(row=2, column=2, padx=5, sticky="w")
self.teCategory = StringVar()
self.teCategory.set(ToDoObj["Category"])
Label(frm, text="Category").grid(row=3, column=1, sticky="e")
frmCat = Frame(frm, width=200, height=25)
frmCat.grid(row=3, column=2, padx=5, sticky="w")
Entry(frmCat, width=30, textvariable=self.teCategory).grid(row=1, column=2, padx=5, sticky="w")
def OnDCatCbSel(event):
self.teCategory.set(self.categoriesCB.get())
self.catSel = StringVar()
self.categoriesCB = ttk.Combobox(frmCat, textvariable=self.catSel)
self.categoriesCB.grid(row=1, column=1, sticky="w", padx=5)
self.categoriesCB['values'] = categories
self.categoriesCB.set(self.teCategory.get())
self.categoriesCB.bind("<<ComboboxSelected>>", OnDCatCbSel)
self.teDescription = StringVar()
self.teDescription.set(ToDoObj["Description"])
Label(frm, text="Description").grid(row=4, column=1, sticky="e")
Entry(frm, width=90, textvariable=self.teDescription).grid(row=4, column=2, padx=5, sticky="w")
self.tePriority = StringVar()
self.tePriority.set(ToDoObj["Priority"])
Label(frm, text="Priority").grid(row=5, column=1, sticky="e")
Entry(frm, width=10, textvariable=self.tePriority).grid(row=5, column=2, padx=5, sticky="w")
#Date (CreationDate)
self.teDate = StringVar()
self.teDate.set(datetime.date.today().strftime("%Y-%m-%d"))
self.teDate.set(ToDoObj["Date"])
Label(frm, text="Date").grid(row=6, column=1, sticky="e")
frmD = Frame(frm, background='#aaa', width=80, height=25)
frmD.grid(row=6, column=2, padx=5, sticky="w")
def selD(e):
self.teDate.set(calD.get_date())
calD = DateEntry(frmD, date_pattern="yyyy-mm-dd")
calD.bind("<<DateEntrySelected>>", selD)
if len((self.teDate.get())) > 1:
calD.set_date(self.teDate.get())
calD.pack(padx=1, pady=1)
#Due-Date
self.teDueDate = StringVar()
self.teDueDate.set(ToDoObj["DueDate"])
Label(frm, text="DueDate").grid(row=7, column=1, sticky="e")
frmDD = Frame(frm, background='#aaa', width=80, height=25)
frmDD.grid(row=7, column=2, padx=5, sticky="w")
def selDD(e):
self.teDueDate.set(calDD.get_date())
calDD = DateEntry(frmDD, date_pattern="yyyy-mm-dd")
calDD.bind("<<DateEntrySelected>>", selDD)
if len((self.teDueDate.get())) > 1:
calDD.set_date(self.teDueDate.get())
calDD.pack(padx=1, pady=1)
self.teStatus = StringVar()
if ToDoObj is not None:
self.teStatus.set(ToDoObj["Status"])
Label(frm, text="Status").grid(row=8, column=1, sticky="e")
Entry(frm, width=40, textvariable=self.teStatus).grid(row=8, column=2, padx=5, sticky="w")
self.teDoneDate = StringVar()
self.teDoneDate.set(ToDoObj["DoneDate"])
Label(frm, text="DoneDate").grid(row=9, column=1, sticky="e")
frmDoneD = Frame(frm, background='#aaa', width=80, height=25)
frmDoneD.grid(row=9, column=2, padx=5, sticky="w")
def selDoneD(e):
self.teDoneDate.set(calDoneD.get_date())
calDoneD = DateEntry(frmDoneD, date_pattern="yyyy-mm-dd")
calDoneD.bind("<<DateEntrySelected>>", selDoneD)
if len((self.teDoneDate.get())) > 1:
calDoneD.set_date(self.teDoneDate.get())
calDoneD.pack(padx=1, pady=1)
Label(frm, text="Progress").grid(row=11, column=1, sticky="e")
self.ProgText = Text(frm, width=80, height=8)
for elem in ToDoObj["Progress"]:
self.ProgText.insert(self.ProgText.index('end'), elem+'\n')
self.ProgText.grid(row=11, column=2, columnspan=4, padx=5, sticky="w")
self.teProgress = StringVar()
self.progEntry = Entry(frm, width=80, textvariable=self.teProgress)
Button(frm, text="+", padx=15, bg="#BFB", command=lambda: self.AddProgress(ToDoObj)).grid(row=12, column=1)
self.progEntry.grid(row=12, column=2, padx=5, sticky="w", columnspan=2)
frmBtn1 = Frame(frm, background='#aaa', width=80, height=25)
frmBtn1.grid(row=14, column=2, padx=5)
b_ok = Button(frmBtn1, text='OK', padx=35, bg="#05CC05", command= lambda: self.save(ToDoObj))
b_ok.grid(row=1, column=2)
b_cancel = Button(frmBtn1, text='Cancel', bg="#D11", command= self.top.destroy)
b_cancel.grid(row=1, column=1)
b_done = Button(frm, text='DONE', bg="#99f", command= lambda: self.done(ToDoObj))
b_done.grid(row=14, column=4)
b_delete = Button(frm, text='delete', bg="#FF3333", command= lambda: self.delete(ToDoObj))
b_delete.grid(row=14, column=1)
def done(self, obj):
obj["DoneDate"] = datetime.date.today().strftime("%Y-%m-%d")
obj["Status"] = "DONE"
writeToDos()
self.top.destroy()
window.reload()
def delete(self, obj):
obj["DoneDate"] = datetime.date.today().strftime("%Y-%m-%d")
obj["Status"] = "DELETED"
writeToDos()
self.top.destroy()
window.reload()
def save(self,ToDoObj):
print("save existing ToDo "+str(ToDoObj['ID']))
ToDoObj["Title"] = self.teTitle.get()
ToDoObj["Category"] = self.teCategory.get()
if self.teCategory.get() not in categories:
categories.append(self.teCategory.get())
window.reload()
ToDoObj["Description"] = self.teDescription.get()
ToDoObj["Priority"] = self.tePriority.get()
ToDoObj["Date"] = self.teDate.get()
ToDoObj["DueDate"] = self.teDueDate.get()
ToDoObj["Status"] = self.teStatus.get()
ToDoObj["DoneDate"] = self.teDoneDate.get()
self.AddProgress(ToDoObj) # Falls noch was im Eingabefeld steht!
ToDoObj["Progress"] = []
for line in self.ProgText.get('1.0', 'end-1c').splitlines():
ToDoObj["Progress"].append(line)
writeToDos()
self.top.destroy()
window.reload()
def AddProgress(self,ToDoObj):
if len(self.teProgress.get()) < 2:
return
newText = datetime.datetime.now().strftime("%y%m%d-%H%M%S ")+self.teProgress.get()
if 'Progress' not in ToDoObj:
ToDoObj['Progress'] = []
ToDoObj['Progress'].append(newText)
self.teProgress.set('')
self.ProgText.insert(self.ProgText.index('end'), newText+'\n')
def getToDoItem(ID):
for item in ItemsObject:
if item['ID'] == ID:
print("getToDoItem("+str(ID)+")",item)
return item
return None
def getNewID():
maxId = 0
for i in ItemsObject:
if i['ID'] > maxId:
maxId = i['ID']
return maxId+1
def writeToDos():
global toDoFile
print('writing ToDos to '+toDoFile)
toDoFO = io.open(toDoFile, mode="w", encoding="utf-8")
json.dump(ItemsObject, toDoFO, sort_keys=False, ensure_ascii=False, indent=2 )
toDoFO.close()
def loadToDoFile():
global ItemsObject
global toDoFile
global categories
toDoFO = io.open(toDoFile, mode="r", encoding="utf-8")
ItemsObject = json.load(toDoFO)
toDoFO.close()
categories = ['ALL']
IDs = []
FixIdsNeeded = False
for item in ItemsObject:
if not "DoneDate" in item:
item["DoneDate"] = ""
if not "Category" in item:
item["Category"] = ""
else:
if item["Category"] not in categories:
categories.append(item["Category"])
if item['ID'] not in IDs:
IDs.append(item['ID'])
else:
print("item "+item['Title'] +' has duplicate ID'+str(item['ID'])+', reset to -1 ! ')
FixIdsNeeded = True
item['ID'] = -1
if FixIdsNeeded:
#reassign IDs
for item in ItemsObject:
if item['ID'] == -1:
item['ID'] = getNewID()
writeToDos()
def reloadFile():
loadToDoFile()
window.reload()
if __name__ == '__main__':
global ItemsObject
global toDoFile
global window
global categories
toDoFile = "ToDo.json"
if len(sys.argv) > 1:
print('using File: '+sys.argv[1])
toDoFile = sys.argv[1]
else:
if not exists(toDoFile):
toDoFile = createNewEmptyJson()
loadToDoFile()
# Start the event loop.
window = Window()
window.mainloop()