-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
445 lines (365 loc) · 14.9 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
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
# Module Imports
import sys
try:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter.font import Font
from PIL import ImageTk, Image
except ModuleNotFoundError:
print("Required modules, Tkinter and Pillow, aren't installed.\nTry running installer.py or install them using pip.")
input("Press Enter to exit")
sys.exit()
try:
from backend.backend import *
except ModuleNotFoundError:
print("Backend files are missing. Re-extract the source code or run the command line in same folder as this file.")
input("Press Enter to exit")
sys.exit()
# Tkinter object
root = Tk()
root.geometry("900x700") # setting Window size
root.resizable(False, False) # Configuring window to unresizeable for frames
root.title('Address Book') # Window title
root.configure(bg="#f9ca74") # Window backgroud colour
root.iconphoto(False, PhotoImage(file='./assets/book.png')) # For window icon
# Required functions
def reset():
# to reset all fields to defaults
global search_entry
search_entry.config(text="")
search_query.set("Select Contact")
FName.set('')
LName.set('')
Contact.set('')
Address.set('')
Email.set('')
Profile.set('')
Gender.set('Select Gender')
profile_pic.pack_forget()
view_frame3.pack_forget()
profile_image.pack_forget()
new_profile_image.pack_forget()
def reset_confirm():
# reset fields with a confirmation
if messagebox.askokcancel("Confirm", "Are you sure want to reset all fields?\nData might not be saved."):
reset()
def destroy(up, down):
# pack and unpack frames for cordination
try:
down.pack_forget()
up.pack(pady=10)
up.focus()
except Exception as e:
messagebox.showerror("Error", f"{e}")
def update_view():
# update contact list
try:
fetch = database.execute("select SNo, FName from AddressBook")
clist.clear()
for record in fetch.fetchall():
clist.append(f"{record[0]} {record[1]}")
if not clist:
clist.append('Select Contact')
search['values'] = clist
except Exception as e:
messagebox.showerror("Error", f"{e}")
def upload_file(profile):
# to upload file and dispaly it
try:
global img
f_types = [('Image Files', ('*.jpg', '*.png'))]
filename = filedialog.askopenfilename(
filetypes=f_types, title="Select profile image")
Profile.set(filename)
img = Image.open(filename)
img = img.resize((75, 100))
img = ImageTk.PhotoImage(img)
profile.pack(side=RIGHT, padx=10)
profile.config(image=img)
profile.image = img
except AttributeError:
# raised if upload dialog was opened but no image selected
pass
except Exception as e:
messagebox.showerror("Error", f"{e}")
def view_data(profile):
# view contact info
try:
index = [int(word)
for word in search_query.get().split() if word.isdigit()]
profile.image = ""
global search_entry
search_entry.config(text=retrieve_data(str(index[0])))
view_frame3.pack(pady=10)
filename = (retrieve_data(index[0], pfp=True))[1]
photo = Image.open(filename)
photo = photo.resize((75, 100))
photo = ImageTk.PhotoImage(photo)
profile.pack(side=RIGHT, padx=10)
profile.config(image=photo)
profile.image = photo
except AttributeError:
# raised if no image path in databse
profile.config(image="")
profile.config(text="No Picture")
profile.pack(side=RIGHT)
except FileNotFoundError:
# raised if data present but cannot be found
messagebox.showerror(
"File not found", "Profile image not found.\nMight have moved or renamed.")
except IndexError:
# raised if nothing is passed to search
messagebox.showerror("Failed", "Empty search query.")
except Exception as e:
messagebox.showerror("Error", f"{e}")
def delete_data():
# delete data from database
try:
if messagebox.askokcancel("Confirm", "Are you sure want to delete?"):
index = [
int(word) for word in search_query.get().split() if word.isdigit()
]
delete_record(str(index[0]))
messagebox.showinfo("Done", "Contact successfully deleted")
update_view()
reset()
view_frame3.pack_forget()
profile_pic.pack_forget()
except Exception as e:
messagebox.showerror("Error", f"{e}")
def edit_data(profile):
# edit data in database
try:
index = [int(word)
for word in search_query.get().split() if word.isdigit()]
profile.image = ""
data = retrieve_all_data(index[0])
FName.set(data[1])
LName.set(data[2])
Gender.set(data[3])
Email.set(data[4])
Contact.set(data[5])
Address.set(data[6])
Profile.set(data[7])
filename = data[7]
photo = Image.open(filename)
photo = photo.resize((75, 100))
photo = ImageTk.PhotoImage(photo)
profile.pack(side=RIGHT)
profile.config(image=photo)
profile.image = photo
except AttributeError:
# raised if no image path in databse
profile.config(image="")
profile.config(text="No Picture")
profile.pack(side=RIGHT)
except FileNotFoundError:
# raised if data present but cannot be found
messagebox.showerror(
"File not found", "Profile image not found.\nMight have moved or renamed.")
except Exception as e:
messagebox.showerror("Error", f"{e}")
def about():
# info about the program
with open('Readme.txt') as f:
lines = f.read()
messagebox.showinfo("About", lines)
# Menu bar
menu = Menu(root)
menu.add_cascade(label="About", command=about) # add about option
menu.add_cascade(label="Exit", command=root.quit) # add exit option
root.config(menu=menu)
# Required variables
FName = StringVar() # carries First Name input
LName = StringVar() # carries Last Name input
Contact = StringVar() # carries contact no. input
Address = StringVar() # carries address input
Email = StringVar() # carries email input
Profile = StringVar() # carries path to profile image input
Gender = StringVar() # carries gender input
search_query = StringVar() # carries search input
clist = [] # contact list
# Tabs
# define welcome frame for welcome panel
welcome_tab = Frame(root, bg="#f9ca74")
home_tab = Frame(root, bg="#f9ca74") # define home frame for home tab
add_tab = Frame(root, bg="#f9ca74") # define add frame for add contact panel
# define view frame for view contact panel
view_tab = Frame(root, bg="#f9ca74")
# define edit frame for edit contact panel
edit_tab = Frame(root, bg="#f9ca74")
# Welcome panel
welcome_tab.pack()
welcome_tab.focus() # frame focus to take input
welcome_image = Image.open("./assets/Welcome.png") # image selection
welcome_image = welcome_image.resize((900, 700))
welcome_image = ImageTk.PhotoImage(welcome_image)
panel = Label(welcome_tab, image=welcome_image) # label to add pic to
panel.image = welcome_image
panel.pack()
# left mouse click to start with home tab
panel.bind('<Button-1>', lambda up: destroy(home_tab, welcome_tab))
# Home tab
Label(home_tab,
text="The Address Book",
font=Font(size=48, slant='italic', underline=True), bg="#f9ca74").pack(anchor='center',
pady=100)
Button(home_tab,
text='View Contact',
font=Font(size=30),
bd='5', bg="#be7d53",
command=lambda: [destroy(view_tab, home_tab),
update_view()]).pack(anchor='center')
Button(home_tab,
text='New Contact',
font=Font(size=30),
bd='5', bg="#be7d53",
command=lambda: [destroy(add_tab, home_tab),
reset()]).pack(anchor='center')
# Add contact tab
add_frame = Frame(add_tab, bg="#f9ca74")
add_frame.pack(pady=10)
add_frame0 = Frame(add_tab, bg="#f9ca74")
add_frame0.pack(pady=10)
add_frame1 = Frame(add_tab, bg="#f9ca74")
add_frame1.pack(pady=10)
add_frame2 = Frame(add_tab, bg="#f9ca74")
add_frame2.pack(pady=10)
add_frame3 = Frame(add_tab, bg="#f9ca74")
add_frame3.pack(pady=10)
add_frame4 = Frame(add_tab, bg="#f9ca74")
add_frame4.pack(pady=10)
add_frame5 = Frame(add_tab, bg="#f9ca74")
add_frame5.pack(pady=10)
add_frame6 = Frame(add_tab, bg="#f9ca74")
add_frame6.pack(pady=10)
add_frame7 = Frame(add_tab, bg="#f9ca74")
add_frame7.pack(pady=10)
Label(add_frame, text="Add Contact", font=Font(size=38,
underline=True), bg="#f9ca74").pack(pady=10)
# Vacant spaces here and in some of the folowing labels have no syntactical use but for symmetry and good appearance
Label(add_frame0, text='First Name', bg="#f9ca74",
justify="right", width=15).pack(side=LEFT)
Entry(add_frame0, textvariable=FName, width=50,
bg="#9bab98").pack(side=RIGHT, pady=5)
Label(add_frame1, text='Last Name', bg="#f9ca74",
justify="right", width=15).pack(side=LEFT)
Entry(add_frame1, textvariable=LName, width=50,
bg="#9bab98").pack(side=RIGHT, pady=5)
Label(add_frame2, text='Phone No.', bg="#f9ca74",
justify="right", width=15).pack(side=LEFT)
Entry(add_frame2, textvariable=Contact, width=50,
bg="#9bab98").pack(side=RIGHT, pady=5)
Label(add_frame3, text='Address', bg="#f9ca74",
justify="right", width=15).pack(side=LEFT)
Entry(add_frame3, textvariable=Address, width=50,
bg="#9bab98").pack(side=RIGHT, pady=5)
Label(add_frame4, text='E-Mail', bg="#f9ca74",
justify="right", width=15).pack(side=LEFT)
Entry(add_frame4, textvariable=Email, width=50,
bg="#9bab98").pack(side=RIGHT, pady=5)
Gender.set("Select Gender")
Label(add_frame5, text='Gender', bg="#f9ca74",
justify="right", width=15).pack(side=LEFT)
# option menu to prevent data from being different than expected
gender_menu = OptionMenu(add_frame5, Gender, "Male", "Female")
gender_menu.pack(side=RIGHT, pady=5)
gender_menu.config(background="#be7d53")
profile_image = Label(add_frame6, bg="#f9ca74")
Button(add_frame6, text='Upload Profile Picture', bg="#be7d53",
command=lambda: upload_file(profile_image)).pack(side=LEFT)
Button(add_frame7,
text="Add", bg="#be7d53",
command=lambda: update_data(FName.get(), LName.get(), Gender.get(
), Address.get(), Email.get(), Contact.get(), Profile.get())).pack(
side=LEFT)
Button(add_frame7, text="Reset", bg="#be7d53",
command=reset_confirm).pack(side=LEFT)
Button(add_frame7, text="Home", bg="#be7d53",
command=lambda: destroy(home_tab, add_tab)).pack(side=LEFT)
# View contact tab
view_frame = Frame(view_tab, bg="#f9ca74")
view_frame.pack(pady=10)
view_frame0 = Frame(view_tab, bg="#f9ca74")
view_frame0.pack(pady=10)
view_frame1 = Frame(view_tab, bg="#f9ca74")
view_frame1.pack(pady=10)
view_frame2 = Frame(view_tab, bg="#f9ca74")
view_frame2.pack(pady=10)
view_frame3 = Frame(view_tab, bg="#f9ca74")
Label(view_frame, text="View Contact", font=Font(size=38,
underline=True), bg="#f9ca74").pack(pady=10)
search_query.set("Select Contact")
search = ttk.Combobox(view_frame0,
textvariable=search_query,
width=50,
values=clist) # combobox with ability to take text input and search along with list selection
search.config(font=Font(size=15), background="#be7d53")
search.pack(side=RIGHT)
search_entry = Label(view_frame2, text="", font=Font(
size=15), justify="left", bg="#f9ca74")
search_entry.pack(side=LEFT)
profile_pic = Label(view_frame2, bg="#be7d53")
Button(view_frame1, text="Search", bg="#be7d53",
command=lambda: view_data(profile_pic)).pack(side=LEFT)
Button(view_frame1, text="Clear", bg="#be7d53", command=reset).pack(side=LEFT)
Button(view_frame1, text="Home", bg="#be7d53",
command=lambda: destroy(home_tab, view_tab)).pack(side=LEFT)
Button(view_frame3, text="Delete", bg="#be7d53",
command=delete_data).pack(side=LEFT)
Button(view_frame3, text="Edit", bg="#be7d53",
command=lambda: [destroy(edit_tab, view_tab), edit_data(new_profile_image)]).pack(side=LEFT)
# Edit contact tab
edit_frame = Frame(edit_tab, bg="#f9ca74")
edit_frame.pack(pady=10)
edit_frame0 = Frame(edit_tab, bg="#f9ca74")
edit_frame0.pack(pady=10)
edit_frame1 = Frame(edit_tab, bg="#f9ca74")
edit_frame1.pack(pady=10)
edit_frame2 = Frame(edit_tab, bg="#f9ca74")
edit_frame2.pack(pady=10)
edit_frame3 = Frame(edit_tab, bg="#f9ca74")
edit_frame3.pack(pady=10)
edit_frame4 = Frame(edit_tab, bg="#f9ca74")
edit_frame4.pack(pady=10)
edit_frame5 = Frame(edit_tab, bg="#f9ca74")
edit_frame5.pack(pady=10)
edit_frame6 = Frame(edit_tab, bg="#f9ca74")
edit_frame6.pack(pady=10)
edit_frame7 = Frame(edit_tab, bg="#f9ca74")
edit_frame7.pack(pady=10)
Label(edit_frame, text="Edit Contact", font=Font(size=38,
underline=True), bg="#f9ca74").pack(pady=10)
# Vacant spaces here and in some of the folowing labels have no syntactical use but for symmetry and good appearance
Label(edit_frame0, text='First Name', bg="#f9ca74", width=15).pack(side=LEFT)
Entry(edit_frame0, textvariable=FName, width=50,
bg="#9bab98").pack(side=RIGHT, pady=5)
Label(edit_frame1, text='Last Name', bg="#f9ca74", width=15).pack(side=LEFT)
Entry(edit_frame1, textvariable=LName, width=50,
bg="#9bab98").pack(side=RIGHT, pady=5)
Label(edit_frame2, text='Phone No.', bg="#f9ca74", width=15).pack(side=LEFT)
Entry(edit_frame2, textvariable=Contact, width=50,
bg="#9bab98").pack(side=RIGHT, pady=5)
Label(edit_frame3, text='Address', bg="#f9ca74", width=15).pack(side=LEFT)
Entry(edit_frame3, textvariable=Address, width=50,
bg="#9bab98").pack(side=RIGHT, pady=5)
Label(edit_frame4, text='E-Mail', bg="#f9ca74", width=15).pack(side=LEFT)
Entry(edit_frame4, textvariable=Email, width=50,
bg="#9bab98").pack(side=RIGHT, pady=5)
Gender.set("Select Gender")
Label(edit_frame5, text='Gender', bg="#f9ca74", width=15).pack(side=LEFT)
gender_menu = OptionMenu(edit_frame5, Gender, "Male", "Female")
gender_menu.pack(side=RIGHT, pady=5)
gender_menu.config(background="#be7d53")
new_profile_image = Label(edit_frame6, bg="#be7d53")
Button(edit_frame6, text='Upload Picture', bg="#be7d53",
command=lambda: upload_file(new_profile_image)).pack(side=LEFT, padx=10)
Button(edit_frame7,
text="Save", bg="#be7d53",
command=lambda: [edit_record(FName.get(), LName.get(), Gender.get(), Email.get(), Contact.get(), Address.get(), Profile.get(), ([int(word) for word in search_query.get().split() if word.isdigit()])[0]), reset(), destroy(view_tab, edit_tab), update_view()]).pack(
side=LEFT)
Button(edit_frame7, text="Discard", bg="#be7d53",
command=lambda: destroy(view_tab, edit_tab)).pack(side=LEFT)
# running the tk window
root.mainloop()