Skip to content

Commit 2e80618

Browse files
committed
re organize pages and name change
1 parent f1381aa commit 2e80618

File tree

1 file changed

+66
-46
lines changed

1 file changed

+66
-46
lines changed

bank_managment_system/QTFrontend.py

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44
import backend
55
backend.connect_database()
66
employee_data = None
7+
# Page Constants (for reference)
8+
HOME_PAGE = 0
9+
ADMIN_PAGE = 1
10+
EMPLOYEE_PAGE = 2
11+
ADMIN_MENU_PAGE = 3
12+
ADD_EMPLOYEE_PAGE = 4
13+
UPDATE_EMPLOYEE_PAGE1 = 5
14+
UPDATE_EMPLOYEE_PAGE2 = 6
15+
EMPLOYEE_LIST_PAGE = 7
16+
ADMIN_TOTAL_MONEY = 8
17+
# -------------------------------------------------------------------------------------------------------------
18+
# === Reusable UI Component Functions ===
19+
# -------------------------------------------------------------------------------------------------------------
20+
721
def create_styled_frame(parent, min_size=None, style=""):
822
"""Create a styled QFrame with optional minimum size and custom style."""
923
frame = QtWidgets.QFrame(parent)
@@ -133,7 +147,23 @@ def on_reject():
133147
button_box.rejected.connect(on_reject)
134148

135149
dialog.exec_()
150+
# -------------------------------------------------------------------------------------------------------------
151+
# === Page Creation Functions ==
152+
# -------------------------------------------------------------------------------------------------------------
153+
def create_page_with_header(parent, title_text):
154+
"""Create a page with a styled header and return the page + main layout."""
155+
page = QtWidgets.QWidget(parent)
156+
main_layout = QtWidgets.QVBoxLayout(page)
157+
main_layout.setContentsMargins(20, 20, 20, 20)
158+
main_layout.setSpacing(20)
136159

160+
header_frame = create_styled_frame(page, style="background-color: #ffffff; border-radius: 10px; padding: 10px;")
161+
header_layout = QtWidgets.QVBoxLayout(header_frame)
162+
title_label = create_styled_label(header_frame, title_text, font_size=30)
163+
header_layout.addWidget(title_label, 0, QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop)
164+
165+
main_layout.addWidget(header_frame, 0, QtCore.Qt.AlignTop)
166+
return page, main_layout
137167
def get_employee_name(parent, name_field_text="Enter Employee Name"):
138168
page, main_layout = create_page_with_header(parent, "Employee Data Update")
139169

@@ -170,7 +200,7 @@ def on_search_button_clicked():
170200
cur.execute("SELECT * FROM staff WHERE name = ?", (entered_name,))
171201
employee_data = cur.fetchone()
172202
print(f"Employee Data: {employee_data}")
173-
parent.setCurrentIndex(6)
203+
parent.setCurrentIndex(UPDATE_EMPLOYEE_PAGE2)
174204

175205
# if employee_data:
176206
# QtWidgets.QMessageBox.information(parent, "Employee Found",
@@ -191,7 +221,7 @@ def on_search_button_clicked():
191221

192222
def create_login_page(parent ,title, name_field_text="Name :", password_field_text="Password :", submit_text="Submit",):
193223
"""Create a login page with a title, name and password fields, and a submit button."""
194-
page, main_layout = create_page_with_header(parent, "Admin Menu")
224+
page, main_layout = create_page_with_header(parent, title)
195225

196226
# Content frame
197227
content_frame = create_styled_frame(page)
@@ -293,21 +323,6 @@ def create_home_page(parent, on_admin_clicked, on_employee_clicked, on_exit_clic
293323

294324
return page
295325

296-
def create_page_with_header(parent, title_text):
297-
"""Create a page with a styled header and return the page + main layout."""
298-
page = QtWidgets.QWidget(parent)
299-
main_layout = QtWidgets.QVBoxLayout(page)
300-
main_layout.setContentsMargins(20, 20, 20, 20)
301-
main_layout.setSpacing(20)
302-
303-
header_frame = create_styled_frame(page, style="background-color: #ffffff; border-radius: 10px; padding: 10px;")
304-
header_layout = QtWidgets.QVBoxLayout(header_frame)
305-
title_label = create_styled_label(header_frame, title_text, font_size=30)
306-
header_layout.addWidget(title_label, 0, QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop)
307-
308-
main_layout.addWidget(header_frame, 0, QtCore.Qt.AlignTop)
309-
return page, main_layout
310-
311326
def create_admin_menu_page(parent):
312327
page, main_layout = create_page_with_header(parent, "Admin Menu")
313328

@@ -392,7 +407,7 @@ def create_add_employee_page(parent, title, submit_text="Submit",update_btn:bool
392407
background-color: #5a6268;
393408
}
394409
""")
395-
back_btn.clicked.connect(lambda: parent.setCurrentIndex(3))
410+
back_btn.clicked.connect(lambda: parent.setCurrentIndex(ADMIN_MENU_PAGE))
396411
main_layout.addWidget(back_btn, 0,alignment=QtCore.Qt.AlignLeft)
397412
if update_btn:
398413
return page, name_edit, password_edit, salary_edit, position_edit, update_button
@@ -471,7 +486,7 @@ def show_employee_list_page(parent, title):
471486
background-color: #5a6268;
472487
}
473488
""")
474-
back_button.clicked.connect(lambda: parent.setCurrentIndex(3))
489+
back_button.clicked.connect(lambda: parent.setCurrentIndex(ADMIN_MENU_PAGE))
475490

476491
content_layout.addWidget(table_frame)
477492
main_layout.addWidget(back_button, alignment=QtCore.Qt.AlignLeft)
@@ -505,10 +520,14 @@ def show_total_money(parent, title):
505520
background-color: #5a6268;
506521
}
507522
""")
508-
back_button.clicked.connect(lambda: parent.setCurrentIndex(3))
523+
back_button.clicked.connect(lambda: parent.setCurrentIndex(ADMIN_MENU_PAGE))
509524
content_layout.addWidget(back_button, alignment=QtCore.Qt.AlignCenter)
510525
main_layout.addWidget(content_frame)
511526
return page
527+
528+
# -------------------------------------------------------------------------------------------------------------
529+
# === Main Window Setup ===
530+
# -------------------------------------------------------------------------------------------------------------
512531

513532
def setup_main_window(main_window):
514533
"""Set up the main window with a stacked widget containing home, admin, and employee pages."""
@@ -523,10 +542,10 @@ def setup_main_window(main_window):
523542

524543
# Create pages
525544
def switch_to_admin():
526-
stacked_widget.setCurrentIndex(1)
545+
stacked_widget.setCurrentIndex(ADMIN_PAGE)
527546

528547
def switch_to_employee():
529-
stacked_widget.setCurrentIndex(2)
548+
stacked_widget.setCurrentIndex(EMPLOYEE_PAGE)
530549

531550
def exit_app():
532551
QtWidgets.QApplication.quit()
@@ -537,7 +556,7 @@ def admin_login_menu_page(name, password):
537556
success = backend.check_admin(name, password)
538557
if success:
539558
QtWidgets.QMessageBox.information(stacked_widget, "Login Successful", f"Welcome, {name}!")
540-
stacked_widget.setCurrentIndex(3)
559+
stacked_widget.setCurrentIndex(ADMIN_MENU_PAGE)
541560
else:
542561
QtWidgets.QMessageBox.warning(stacked_widget, "Login Failed", "Incorrect name or password.")
543562
except Exception as e:
@@ -606,37 +625,37 @@ def update_employee_data(name, password, salary, position, name_to_update):
606625
stacked_widget
607626
)
608627

609-
add_button.clicked.connect(lambda: stacked_widget.setCurrentIndex(4))
610-
update_button.clicked.connect(lambda: stacked_widget.setCurrentIndex(5))
611-
list_button.clicked.connect(lambda: stacked_widget.setCurrentIndex(7))
612-
back_button.clicked.connect(lambda: stacked_widget.setCurrentIndex(0))
613-
money_button.clicked.connect(lambda: stacked_widget.setCurrentIndex(8))
628+
add_button.clicked.connect(lambda: stacked_widget.setCurrentIndex(ADD_EMPLOYEE_PAGE))
629+
update_button.clicked.connect(lambda: stacked_widget.setCurrentIndex(UPDATE_EMPLOYEE_PAGE))
630+
list_button.clicked.connect(lambda: stacked_widget.setCurrentIndex(EMPLOYEE_LIST_PAGE))
631+
back_button.clicked.connect(lambda: stacked_widget.setCurrentIndex(HOME_PAGE))
632+
money_button.clicked.connect(lambda: stacked_widget.setCurrentIndex(ADMIN_TOTAL_MONEY))
614633
# Create Add Employee Page
615634
add_employee_page, emp_name, emp_password, emp_salary, emp_position, emp_submit = create_add_employee_page(
616635
stacked_widget,
617636
title="Add Employee"
618637
)
619638

620639
# Update Employee Page
621-
update_employee_page1 = get_employee_name(stacked_widget)
640+
u_employee_page1 = get_employee_name(stacked_widget)
622641
# apply the update_employee_data function to the submit button
623642

624-
update_employee_page2 ,update_employee_name, update_employee_password, update_employee_salary, update_employee_position,update_employee_update = create_add_employee_page(stacked_widget,"Update Employee Details",update_btn=True)
643+
u_employee_page2 ,u_employee_name, u_employee_password, u_employee_salary, u_employee_position,u_employee_update = create_add_employee_page(stacked_widget,"Update Employee Details",update_btn=True)
625644
def populate_employee_data():
626645
global employee_data
627646
if employee_data:
628647
print("employee_data is not None")
629-
update_employee_name.setText(str(employee_data[0])) # Name
630-
update_employee_password.setText(str(employee_data[1])) # Password
631-
update_employee_salary.setText(str(employee_data[2])) # Salary
632-
update_employee_position.setText(str(employee_data[3])) # Position
648+
u_employee_name.setText(str(employee_data[0])) # Name
649+
u_employee_password.setText(str(employee_data[1])) # Password
650+
u_employee_salary.setText(str(employee_data[2])) # Salary
651+
u_employee_position.setText(str(employee_data[3])) # Position
633652
else:
634653
# Clear fields if no employee data is available
635654
print("employee_data is None")
636-
update_employee_name.clear()
637-
update_employee_password.clear()
638-
update_employee_salary.clear()
639-
update_employee_position.clear()
655+
u_employee_name.clear()
656+
u_employee_password.clear()
657+
u_employee_salary.clear()
658+
u_employee_position.clear()
640659
QtWidgets.QMessageBox.warning(stacked_widget, "No Data", "No employee data available to display.")
641660
def on_page_changed(index):
642661
if index == 6: # update_employee_page2 is at index 6
@@ -668,12 +687,12 @@ def update_employee_data(name, password, salary, position, name_to_update):
668687
show_popup_message(stacked_widget, "Employee updated successfully.", 3, False)
669688
except Exception as e:
670689
show_popup_message(stacked_widget, f"Error updating employee: {str(e)}", 5)
671-
update_employee_update.clicked.connect(
690+
u_employee_update.clicked.connect(
672691
lambda: update_employee_data(
673-
update_employee_name.text().strip(),
674-
update_employee_password.text().strip(),
675-
update_employee_salary.text().strip(),
676-
update_employee_position.text().strip(),
692+
u_employee_name.text().strip(),
693+
u_employee_password.text().strip(),
694+
u_employee_salary.text().strip(),
695+
u_employee_position.text().strip(),
677696
employee_data[0] if employee_data else ""
678697
)
679698
)
@@ -703,8 +722,8 @@ def update_employee_data(name, password, salary, position, name_to_update):
703722
stacked_widget.addWidget(employee_page)#2
704723
stacked_widget.addWidget(admin_menu_page)#3
705724
stacked_widget.addWidget(add_employee_page)#4
706-
stacked_widget.addWidget(update_employee_page1)#5
707-
stacked_widget.addWidget(update_employee_page2)#6
725+
stacked_widget.addWidget(u_employee_page1)#5
726+
stacked_widget.addWidget(u_employee_page2)#6
708727
stacked_widget.addWidget(employee_list_page)#7
709728
stacked_widget.addWidget(admin_total_money)#8
710729

@@ -714,7 +733,7 @@ def update_employee_data(name, password, salary, position, name_to_update):
714733
main_window.setCentralWidget(central_widget)
715734

716735
# Set initial page
717-
stacked_widget.setCurrentIndex(3)
736+
stacked_widget.setCurrentIndex(EMPLOYEE_PAGE)
718737

719738
return stacked_widget, {
720739
"admin_name": admin_name,
@@ -736,6 +755,7 @@ def main():
736755

737756
main_window.show()
738757
sys.exit(app.exec_())
758+
# -------------------------------------------------------------------------------------------------------------
739759

740760
if __name__ == "__main__":
741761
main()

0 commit comments

Comments
 (0)