-
Notifications
You must be signed in to change notification settings - Fork 2
/
TODOApp.py
88 lines (75 loc) · 2.72 KB
/
TODOApp.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
#!/usr/bin/env python
# This is a small fun project which I have developed to get through all the concepts learned so far.
# For installing on Ubuntu:
# sudo apt install python3-tk
from tkinter import *
from tkinter.messagebox import showerror
import pickle
class todoGUI(object):
def __init__(self):
self.mainWindow = Tk()
self.mainWindow.title('TODO Application!')
self.fieldnames = ('key', 'name', 'age', 'pay')
def buildWidgets(self):
global entries
entries = {}
self.mainFrame = Frame(self.mainWindow)
self.mainFrame.pack()
for(idx, label) in enumerate(self.fieldnames):
labels = Label(self.mainFrame, text = label)
ent = Entry(self.mainFrame, font=('Arial', 17))
# ent.pack()
labels.grid(row = idx, column = 0)
ent.grid(row = idx, column = 1)
entries[label] = ent
Button(self.mainWindow, text="Fetch", command = self.fetchRecord).pack(side=LEFT)
Button(self.mainWindow, text="Update", command = self.updateRecord).pack(side=LEFT)
Button(self.mainWindow, text="Quit", command = self.mainWindow.quit).pack(side=RIGHT)
return entries
def fetchRecord(self):
dbfile = open('examplePickle', 'rb')
db = pickle.load(dbfile)
try:
key = entries['key'].get()
record = db[key]
# fetch by key, show in GUI
except:
showerror(title='Error', message='No such key!')
else:
for field in self.fieldnames:
entries[field].delete(0, END)
entries[field].insert(0, record[field])
dbfile.close()
return record
def updateRecord(self):
dbfile = open('examplePickle', 'rb')
db = pickle.load(dbfile)
dbfile.close()
key = entries['key'].get()
if key in db:
record = db[key]
# update existing record
else:
# make/store new one for key
# db['name'] = entries['name'].get()
# db[str(key)] = key
dbfile = open('examplePickle', 'ab')
temp = {}
# temp['key'] = str(key)
# for field in self.fieldnames[1:]:
# temp[field] = str(entries[field].get())
#
# print(temp)
# db[str(key)] = temp
# print(db)
del db[key]
for field in self.fieldnames:
temp[field] = entries[field].get()
pickle.dump(temp, dbfile)
dbfile.close()
def startGUI(self):
self.mainWindow.mainloop()
if __name__ == '__main__':
window = todoGUI()
entries = window.buildWidgets()
window.startGUI()