-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
580 lines (479 loc) · 22.5 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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
"""
This program is developed by Habeeb Rahman K T (VTU22336)
"""
import sqlite3
import PySimpleGUI as Sg
import sys
import os
BUNDLE_DIRECTORY = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__)))
CREDENTIAL_PATH = "credential.db"
STUDENTS_DATA_PATH = "students.db"
LOGO_PATH = BUNDLE_DIRECTORY + "\\logo.png"
# Hiding files
# subprocess.check_call(["attrib", "+H", CREDENTIAL_PATH])
# subprocess.check_call(["attrib", "+H", STUDENTS_DATA_PATH])
WINDOW_CLOSED = 'Window is closed'
ACCESS_GRANTED = 'Username and password is correct'
ACCESS_DENIED = 'incorrect password or username'
NOT_FOUND = 'NO_DATA_FOUND'
def extract_roll_number_from_data(data):
roll_number = NOT_FOUND
try:
roll_number = int(data[0][0:5])
except ValueError:
print('Value Error!')
return roll_number
# This function gets all the details of a student from database
# by taking student roll number as argument
# and returns a tuple of details ONLY if the roll number is present in the table
def get_details_with_roll_no(_roll_number):
try:
cursor.execute(f"SELECT * FROM students_details WHERE Roll_No = {_roll_number}")
details = cursor.fetchone()
except sqlite3.OperationalError:
Sg.popup_ok("Enter a valid roll number to continue")
else:
return details
return None
# Beautifying data for the user
def justify_details(details):
longest_student_name_length = 4
longest_father_name_length = 13
longest_mother_name_length = 13
longest_address_length = 7
for student_details in details:
if longest_student_name_length < len(student_details[1]):
longest_student_name_length = len(student_details[1])
if longest_father_name_length < len(student_details[3]):
longest_father_name_length = len(student_details[3])
if longest_mother_name_length < len(student_details[4]):
longest_mother_name_length = len(student_details[4])
if longest_address_length < len(student_details[5]):
longest_address_length = len(student_details[5])
# Making the details of the student cleaner to view
students_details_to_view = []
for student_details in details:
blank_space_after_student_name = longest_student_name_length - len(student_details[1]) + 8
blank_space_after_father_name = longest_father_name_length - len(student_details[3]) + 8
blank_space_after_mother_name = longest_mother_name_length - len(student_details[4]) + 8
blank_space_after_address = longest_address_length - len(student_details[5]) + 8
edited_data = f'{student_details[0]} : ' \
f'{student_details[1]} {blank_space_after_student_name * " "}' \
f'{student_details[2]}{" " * 8}' \
f'{student_details[3]} {blank_space_after_father_name * " "}' \
f'{student_details[4]} {blank_space_after_mother_name * " "}' \
f'{student_details[5]} {blank_space_after_address * " "}' \
f'{student_details[6]}{" " * 8}' \
f'{student_details[7]}'
students_details_to_view.append(edited_data)
students_details_to_view.insert(0, f'ROLL NO '
f'NAME{(longest_student_name_length + 5) * " "}'
f'DOB{" " * 15}'
f'FATHER\'S NAME{(longest_father_name_length - 4) * " "}'
f'MOTHER\'S NAME{(longest_mother_name_length - 4) * " "}'
f'ADDRESS{(longest_address_length + 2) * " "}'
f'AADHAAR NO. {" " * 8}'
f'BANK ACCOUNT NO.')
students_details_to_view.insert(1, " ")
return students_details_to_view
def get_all_students():
cursor.execute('SELECT * FROM students_details')
students = cursor.fetchall()
if not students:
return NOT_FOUND
justified_students_details = justify_details(students)
return justified_students_details
def delete_student(roll_no):
cursor.execute(f'DELETE FROM students_details WHERE Roll_No = {roll_no}')
student_db_conn.commit()
# Window for viewing the list of all students
def show_all_students():
students = get_all_students()
if students == NOT_FOUND:
students = ['No student added...']
window_layout = [
[
Sg.Text("Enter Student's Roll No. : "),
Sg.Input(key='STUDENT_ON_ROLL'),
Sg.Button(key='SEARCH', button_text='Search'),
Sg.Button(key='CLEAR', button_text='Clear filter', visible=False)
],
[Sg.Listbox(key='STUDENTS_LIST', values=students, expand_x=True, pad=(0, (20, 0)),
expand_y=True, font=('Courier', 12), enable_events=True,
right_click_menu=['', ['View Details', 'Edit', 'Delete']], horizontal_scroll=True)]
]
window = Sg.Window('List of students', layout=window_layout, font=("Gill Sans MT", 11),
finalize=True, resizable=True)
window.maximize()
while True:
event, values = window.read()
if event == 'Exit' or event == Sg.WIN_CLOSED:
window.close()
break
elif event == 'View Details':
roll_number = extract_roll_number_from_data(values['STUDENTS_LIST'])
if roll_number != NOT_FOUND:
student_details = get_details_with_roll_no(roll_number)
print(type(student_details))
edited_details = f"Name : {student_details[1]}\n\n" \
f"Date Of Birth : {student_details[2]}\n\n" \
f"Fathers Name : {student_details[3]}\n\n" \
f"Mothers Name : {student_details[4]}\n\n" \
f"Address : {student_details[5]}\n\n" \
f"Aadhaar Number : {student_details[6]}\n\n" \
f"Account Number : {student_details[7]}"
Sg.popup(edited_details, custom_text='Cancel', no_titlebar=True,
font=('Courier', 14), grab_anywhere=False)
elif event == 'Edit':
roll_number = extract_roll_number_from_data(values['STUDENTS_LIST'])
if roll_number != NOT_FOUND:
window.close()
edit_student_window(roll_number)
show_all_students()
elif event == 'Delete':
roll_number = extract_roll_number_from_data(values['STUDENTS_LIST'])
if roll_number != NOT_FOUND:
deletion_details = get_details_with_roll_no(roll_number)
text_to_show = f'Confirm to delete student with\n' \
f'Roll number : {roll_number}\n' \
f'Name : {deletion_details[1]}'
user_conformation = Sg.popup_ok_cancel(text_to_show)
if user_conformation == 'OK':
delete_student(roll_number)
students = get_all_students()
window['STUDENTS_LIST'].update(values=students)
elif event == 'SEARCH':
_roll_number = values['STUDENT_ON_ROLL']
if _roll_number == '':
window['STUDENTS_LIST'].update(values=students)
continue
details = get_details_with_roll_no(_roll_number)
if details is None:
Sg.popup_ok("No student is registered under this roll number")
else:
details = [tuple(details)]
justified_student_details = justify_details(details)
window['STUDENTS_LIST'].update(values=justified_student_details)
window['SEARCH'].update(visible=False)
window['CLEAR'].update(visible=True)
elif event == 'CLEAR':
window['STUDENTS_LIST'].update(values=students)
window['CLEAR'].update(visible=False)
window['SEARCH'].update(visible=True)
# Window for editing details of an existing student
def edit_student_window(roll_number=NOT_FOUND):
edit_window_layout = [
[
Sg.Text("Enter Student's Roll No. : ", key='ON_ROLL_MESSAGE', pad=(5, 20)),
Sg.Input(key='STUDENT_ON_ROLL'),
Sg.Submit(key='ON_ROLL_SUBMIT')
],
[
Sg.Text("Full Name : ", expand_x=True),
Sg.InputText(key="NAME", default_text="")
],
[
Sg.Text("Date Of Birth (YYYY-MM-DD) : ", expand_x=True),
Sg.InputText(key="DOB")
],
[
Sg.Text("Father's Name : ", expand_x=True),
Sg.InputText(key="FATHER_NAME")
],
[
Sg.Text("Mother's Name : ", expand_x=True),
Sg.InputText(key="MOTHER_NAME")
],
[
Sg.Text("Address : ", expand_x=True),
Sg.InputText(key="ADDRESS")
],
[
Sg.Text("Aadhaar Number : ", expand_x=True),
Sg.InputText(key="AADHAAR_NO")
],
[
Sg.Text("Bank Account Number : ", expand_x=True),
Sg.InputText(key="BANK_ACCOUNT_NO")
],
[
Sg.Submit(key="EDIT_SUBMIT", pad=(5, 50))
]
]
edit_window = Sg.Window(title="View Student Detail", layout=edit_window_layout, font=("Gill Sans MT", 11),
resizable=True, finalize=True)
edit_window.maximize()
if roll_number != NOT_FOUND:
edit_window['ON_ROLL_MESSAGE'].update(visible=False)
edit_window['ON_ROLL_SUBMIT'].update(visible=False)
# try:
details = get_details_with_roll_no(roll_number)
# except Exception:
# Sg.popup_ok("Enter A Valid Roll Number To Continue")
# else:
if details is None:
Sg.popup_ok("No Student Is Registered Under This Roll Number")
else:
edit_window['STUDENT_ON_ROLL'].update(roll_number)
edit_window['NAME'].update(details[1])
edit_window['DOB'].update(details[2])
edit_window['FATHER_NAME'].update(details[3])
edit_window['MOTHER_NAME'].update(details[4])
edit_window['ADDRESS'].update(details[5])
edit_window['AADHAAR_NO'].update(details[6])
edit_window['BANK_ACCOUNT_NO'].update(details[7])
while True:
event, values = edit_window.read()
if event == 'Exit' or event == Sg.WIN_CLOSED:
edit_window.close()
break
elif event == 'ON_ROLL_SUBMIT':
# if roll_number == NOT_FOUND:
roll_number = values['STUDENT_ON_ROLL']
try:
details = get_details_with_roll_no(roll_number)
except sqlite3.OperationalError:
Sg.popup_ok("Entered roll number is invalid")
continue
else:
try:
edit_window['NAME'].update(details[1])
edit_window['DOB'].update(details[2])
edit_window['FATHER_NAME'].update(details[3])
edit_window['MOTHER_NAME'].update(details[4])
edit_window['ADDRESS'].update(details[5])
edit_window['AADHAAR_NO'].update(details[6])
edit_window['BANK_ACCOUNT_NO'].update(details[7])
except TypeError:
Sg.popup_ok("No student is registered under this roll number")
continue
elif event == 'EDIT_SUBMIT':
_roll_number = values['STUDENT_ON_ROLL']
details = get_details_with_roll_no(_roll_number)
if details is not None:
new_details = (int(values['STUDENT_ON_ROLL']),
values['NAME'],
values['DOB'],
values['FATHER_NAME'],
values['MOTHER_NAME'],
values['ADDRESS'],
values['AADHAAR_NO'],
values['BANK_ACCOUNT_NO'])
if new_details == details:
Sg.popup_ok("No data changed")
continue
Sg.popup_ok("Confirm to change data")
cursor.execute(f"DELETE FROM students_details WHERE Roll_No = {_roll_number}")
add_student_query = "INSERT INTO students_details (Roll_No, Name, " \
"Date_Of_Birth, Fathers_Name, Mothers_Name, Address, Aadhaar_No, " \
"Bank_Account_Number)" \
" VALUES (?, ?, date(?), ?, ?, ?, ?, ?) "
try:
cursor.execute(add_student_query, (_roll_number, # Roll Number
new_details[1], # Name
new_details[2], # Date Of Birth
new_details[3], # Father's Name
new_details[4], # Mother's Name
new_details[5], # Address
new_details[6], # Aadhaar Number
new_details[7])) # Bank Account Number
student_db_conn.commit()
except sqlite3.IntegrityError:
Sg.popup_ok("Check the format of date of birth (YYYY-MM-DD)")
else:
Sg.popup_ok("Data Changed!")
# Window for Adding A Student
def add_student_window():
add_student_layout = [
[
Sg.Text("Enter Following Details", justification="center/top",
font=("Gill Sans MT", 13), expand_x=True, pad=(0, 16))
],
[
Sg.Text("Roll Number : ", expand_x=True),
Sg.InputText(key="ROLL_NO", pad=(40, 8))
],
[
Sg.Text("Full Name : ", expand_x=True),
Sg.InputText(key="NAME", pad=(40, 8))
],
[
Sg.Text("Date Of Birth (YYYY-MM-DD) : ", expand_x=True),
Sg.InputText(key="DOB", pad=(40, 8))
],
[
Sg.Text("Father's Name : ", expand_x=True),
Sg.InputText(key="FATHER_NAME", pad=(40, 8))
],
[
Sg.Text("Mother's Name : ", expand_x=True),
Sg.InputText(key="MOTHER_NAME", pad=(40, 8))
],
[
Sg.Text("Address : ", expand_x=True),
Sg.InputText(key="ADDRESS", pad=(40, 8))
],
[
Sg.Text("Aadhaar Number : ", expand_x=True),
Sg.InputText(key="AADHAAR_NO", pad=(40, 8))
],
[
Sg.Text("Bank Account Number : ", expand_x=True),
Sg.InputText(key="BANK_ACCOUNT_NO", pad=(40, 8))
],
[
Sg.Submit(key="SUBMIT", pad=(5, 50))
]
]
add_window = Sg.Window(title="Add Student", layout=add_student_layout, element_padding=(5, 5), resizable=True,
font=("Gill Sans MT", 11)) \
.finalize()
add_window.maximize()
while True:
event, values = add_window.read()
if event == 'Exit' or event == Sg.WIN_CLOSED:
add_window.close()
break
elif event == "SUBMIT":
try:
roll_no = int(values["ROLL_NO"])
except ValueError:
Sg.popup_ok('Enter a valid roll number')
continue
name = values["NAME"].lstrip(' ')
fathers_name = values["FATHER_NAME"].lstrip(' ')
mothers_name = values["MOTHER_NAME"].lstrip(' ')
address = values["ADDRESS"].lstrip(' ')
aadhaar_no = values["AADHAAR_NO"].lstrip(' ')
bank_account_no = values["BANK_ACCOUNT_NO"].lstrip(' ')
if roll_no == 0:
Sg.popup_ok('Zero cannot be assigned as a roll number')
continue
if (name, fathers_name, mothers_name, address) == ('', '', '', ''):
Sg.popup_ok('Please fill the form completely')
continue
if len(aadhaar_no) != 12:
Sg.popup_ok('Please check your Aadhaar number')
continue
# try:
# date_of_birth = datetime.strptime(values["DOB"], "%Y-%m-%d").date()
# except ValueError:
# Sg.popup_ok("Check format of field Date Of Birth")
else:
add_student_query = "INSERT INTO students_details (Roll_No, Name, Date_Of_Birth, " \
"Fathers_Name, Mothers_Name, Address, Aadhaar_No, Bank_Account_Number)" \
" VALUES (?,?,date(?),?,?,?,?,?) "
try:
cursor.execute(add_student_query, (roll_no, name, values['DOB'], fathers_name,
mothers_name, address, aadhaar_no, bank_account_no))
student_db_conn.commit()
except sqlite3.IntegrityError:
Sg.popup_ok("hAnother student is already registered under this roll number!")
else:
add_window.close()
break
# Main window of the application
def home_window():
layout = [
[
Sg.Text("Student Management System", font=('Gill Sans MT', 24), expand_x=True, justification="centre"),
],
[Sg.Image(source=LOGO_PATH, expand_y=True, expand_x=True)],
[
Sg.Button(key="ADD_BUTTON", button_text="Add a student", enable_events=True, expand_x=True),
Sg.Button(key="EDIT_BUTTON", button_text="Edit a student", enable_events=True, expand_x=True),
Sg.Button(key='STUDENTS_LIST', button_text='Show all students', enable_events=True, expand_x=True)
],
# [Sg.Text('This software is developed and managed by students of GBSSS (Computer Science)',
# pad=(0, (8, 0)), font=('Gill Sans MT', 8))]
]
window = Sg.Window(title="Students Management", layout=layout, size=(1000, 450), element_padding=(5, 10),
default_button_element_size=(10, 1), font=('Gill Sans MT', 12))
while True:
event, value = window.read()
if event == 'Exit' or event == Sg.WIN_CLOSED:
break
elif event == "ADD_BUTTON":
window.close() # Main window is closed -> clicks 'Add Student' button for a new window
add_student_window() # Calling to show window to ADD A STUDENT
home_window() # Recalling the main window, to prevent whole software from closing
elif event == "EDIT_BUTTON":
window.close() # Main window is closed -> clicks 'Add Student' button for a new window
edit_student_window() # Calling to show window to EDIT STUDENT DETAILS
home_window() # Recalling the main window, to prevent whole software from closing
elif event == "STUDENTS_LIST":
window.close() # Main window is closed -> clicks 'Add Student' button for a new window
show_all_students() # Calling to show window to EDIT STUDENT DETAILS
home_window() # Recalling the main window, to prevent whole software from closing
window.close()
# Password Window For Confirming The User
def get_authenticated():
title = "Sign Up"
login_conn = sqlite3.connect(CREDENTIAL_PATH)
login_cursor = login_conn.cursor()
try:
login_cursor.execute("""CREATE TABLE credentials (
username TEXT,
password TEXT)""")
except sqlite3.OperationalError:
print("Credential database already exist")
title = "Login"
finally:
password_window_layout = [
[
Sg.Text('Username : ', pad=(5, (30, 5))),
Sg.Input(key='USERNAME', pad=(5, (30, 5)))
],
[
Sg.Text("Password : "),
Sg.Input(key="PASSWORD")
],
[Sg.Submit(key='PASSWORD_SUBMIT', button_text=title, pad=(5, (30, 10)))]
]
password_window = Sg.Window(title=title, layout=password_window_layout, font=('Gill Sans MT', 12),
element_justification='center')
while True:
event, values = password_window.read()
if event == 'Exit' or event == Sg.WIN_CLOSED:
password_window.close()
return WINDOW_CLOSED
elif event == 'PASSWORD_SUBMIT':
username = values['USERNAME']
password = values['PASSWORD']
# If the user has already entered the credentials
if title == "Login":
login_cursor.execute(f"""SELECT password FROM credentials
WHERE username = ?""", (username,))
saved_pass = login_cursor.fetchone()
if saved_pass is not None and password == saved_pass[0]:
login_conn.close()
password_window.close()
return True
else:
Sg.popup_ok("Please enter a valid Username and Password")
else:
login_cursor.execute("""INSERT INTO CREDENTIALS VALUES(?, ?)""", (username, password))
login_conn.commit()
login_conn.close()
password_window.close()
return True
Sg.theme("LightBlue3")
if __name__ == "__main__":
access_granted = get_authenticated()
if access_granted and access_granted != WINDOW_CLOSED:
student_db_conn = sqlite3.connect(STUDENTS_DATA_PATH)
cursor = student_db_conn.cursor()
try:
cursor.execute("""CREATE TABLE students_details (
Roll_No TEXT PRIMARY KEY ,
Name TEXT NOT NULL,
Date_Of_Birth TEXT NOT NULL,
Fathers_Name TEXT,
Mothers_Name TEXT,
Address TEXT,
Aadhaar_No VARCHAR(12) NOT NULL UNIQUE,
Bank_Account_Number TEXT)""")
except sqlite3.OperationalError:
print("Database already exist")
finally:
home_window()