Skip to content

Commit 1a4a919

Browse files
committed
reduce code and improve it by reusable function block
1 parent d285037 commit 1a4a919

File tree

1 file changed

+126
-142
lines changed

1 file changed

+126
-142
lines changed

bank_managment_system/QTFrontend.py

Lines changed: 126 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,15 @@ def create_input_field(parent, label_text, min_label_size=(120, 0)):
6868
layout.addWidget(label)
6969
layout.addWidget(line_edit)
7070
return frame, line_edit
71-
def pop_up_message(parent, message: str, page: int):
72-
"""Create a popup message box with a given message."""
71+
def show_popup_message(parent, message: str, page: int = None, show_cancel: bool = True):
72+
"""Reusable popup message box.
73+
74+
Args:
75+
parent: The parent widget.
76+
message (str): The message to display.
77+
page (int, optional): Page index to switch to after dialog closes.
78+
show_cancel (bool): Whether to show the Cancel button.
79+
"""
7380
dialog = QtWidgets.QDialog(parent)
7481
dialog.setWindowTitle("Message")
7582
dialog.setFixedSize(350, 100)
@@ -84,9 +91,14 @@ def pop_up_message(parent, message: str, page: int):
8491
label.setWordWrap(True)
8592
layout.addWidget(label)
8693

87-
button_box = QtWidgets.QDialogButtonBox(
88-
QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel
89-
)
94+
# Decide which buttons to show
95+
if show_cancel:
96+
button_box = QtWidgets.QDialogButtonBox(
97+
QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel
98+
)
99+
else:
100+
button_box = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok)
101+
90102
button_box.setStyleSheet("""
91103
QPushButton {
92104
background-color: #3498db;
@@ -104,66 +116,24 @@ def pop_up_message(parent, message: str, page: int):
104116
""")
105117
layout.addWidget(button_box)
106118

107-
button_box.accepted.connect(dialog.accept)
108-
button_box.rejected.connect(lambda: reject_clicked(dialog, parent, page))
109-
110-
dialog.exec_()
111-
112-
def reject_clicked(dialog, parent, page):
113-
parent.setCurrentIndex(page)
114-
dialog.reject()
115-
116-
def pop_up_message_with_only_ok(parent, message: str, page: int):
117-
"""Create a popup message box with only an OK button."""
118-
dialog = QtWidgets.QDialog(parent)
119-
dialog.setWindowTitle("Message")
120-
dialog.setFixedSize(350, 100)
121-
dialog.setStyleSheet("background-color: #f0f0f0;")
119+
# Connect buttons
120+
def on_accept():
121+
if page is not None:
122+
parent.setCurrentIndex(page)
123+
dialog.accept()
122124

123-
layout = QtWidgets.QVBoxLayout(dialog)
124-
layout.setSpacing(10)
125-
layout.setContentsMargins(15, 15, 15, 15)
126-
127-
label = QtWidgets.QLabel(message)
128-
label.setStyleSheet("font-size: 12px; color: #2c3e50;")
129-
label.setWordWrap(True)
130-
layout.addWidget(label)
131-
132-
button_box = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok)
133-
button_box.setStyleSheet("""
134-
QPushButton {
135-
background-color: #3498db;
136-
color: white;
137-
border-radius: 4px;
138-
padding: 6px 12px;
139-
min-width: 80px;
140-
}
141-
QPushButton:hover {
142-
background-color: #2980b9;
143-
}
144-
QPushButton:pressed {
145-
background-color: #1c6ea4;
146-
}
147-
""")
148-
layout.addWidget(button_box)
125+
def on_reject():
126+
if page is not None:
127+
parent.setCurrentIndex(page)
128+
dialog.reject()
149129

150-
button_box.accepted.connect(lambda: accepted_clicked())
151-
def accepted_clicked():
152-
parent.setCurrentIndex(page)
153-
dialog.close()
130+
button_box.accepted.connect(on_accept)
131+
button_box.rejected.connect(on_reject)
154132

155133
dialog.exec_()
156134
def create_login_page(parent ,title, name_field_text="Name :", password_field_text="Password :", submit_text="Submit",):
157135
"""Create a login page with a title, name and password fields, and a submit button."""
158-
page = QtWidgets.QWidget(parent)
159-
main_layout = QtWidgets.QVBoxLayout(page)
160-
161-
# Header frame with title
162-
header_frame = create_styled_frame(page, style="background-color: #ffffff; border-radius: 10px; padding: 10px;")
163-
header_layout = QtWidgets.QVBoxLayout(header_frame)
164-
title_label = create_styled_label(header_frame, title, font_size=30)
165-
header_layout.addWidget(title_label, 0, QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop)
166-
main_layout.addWidget(header_frame, 0, QtCore.Qt.AlignTop)
136+
page, main_layout = create_page_with_header(parent, "Admin Menu")
167137

168138
# Content frame
169139
content_frame = create_styled_frame(page)
@@ -203,23 +173,13 @@ def on_login_button_clicked(parent,name_field, password_field):
203173
# Check if the entered name and password are correct
204174
if name == "" and password == "":
205175
# Show a message box with the entered name and password
206-
pop_up_message(parent, "Please enter your name and password.",0)
176+
show_popup_message(parent, "Please enter your name and password.",0)
207177
else:
208178
print(f"Name: {name}, Password: {password}")
209179

210180
def create_home_page(parent, on_admin_clicked, on_employee_clicked, on_exit_clicked):
211181
"""Create the home page with Admin, Employee, and Exit buttons."""
212-
page = QtWidgets.QWidget(parent)
213-
main_layout = QtWidgets.QVBoxLayout(page)
214-
main_layout.setContentsMargins(20, 20, 20, 20)
215-
main_layout.setSpacing(20)
216-
217-
# Header frame with title
218-
header_frame = create_styled_frame(page, style="background-color: #ffffff; border-radius: 10px; padding: 10px;")
219-
header_layout = QtWidgets.QVBoxLayout(header_frame)
220-
title_label = create_styled_label(header_frame, "Bank Management System", font_size=30)
221-
header_layout.addWidget(title_label, 0, QtCore.Qt.AlignHCenter)
222-
main_layout.addWidget(header_frame, 0, QtCore.Qt.AlignTop)
182+
page, main_layout = create_page_with_header(parent, "Admin Menu")
223183

224184
# Button frame
225185
button_frame = create_styled_frame(page)
@@ -267,95 +227,78 @@ def create_home_page(parent, on_admin_clicked, on_employee_clicked, on_exit_clic
267227
exit_button.clicked.connect(on_exit_clicked)
268228

269229
return page
270-
271-
def create_admin_menu_page(perent):
272-
"""Create the admin menu page with buttons for adding, deleting, and viewing accounts."""
273-
page = QtWidgets.QWidget(perent)
230+
def create_page_with_header(parent, title_text):
231+
"""Create a page with a styled header and return the page + main layout."""
232+
page = QtWidgets.QWidget(parent)
274233
main_layout = QtWidgets.QVBoxLayout(page)
275234
main_layout.setContentsMargins(20, 20, 20, 20)
276235
main_layout.setSpacing(20)
277236

278-
# Header frame with title
279237
header_frame = create_styled_frame(page, style="background-color: #ffffff; border-radius: 10px; padding: 10px;")
280238
header_layout = QtWidgets.QVBoxLayout(header_frame)
281-
title_label = create_styled_label(header_frame, "Admin Menu", font_size=30)
282-
header_layout.addWidget(title_label, 0, QtCore.Qt.AlignHCenter)
239+
title_label = create_styled_label(header_frame, title_text, font_size=30)
240+
header_layout.addWidget(title_label, 0, QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop)
241+
283242
main_layout.addWidget(header_frame, 0, QtCore.Qt.AlignTop)
243+
return page, main_layout
244+
def create_admin_menu_page(parent):
245+
page, main_layout = create_page_with_header(parent, "Admin Menu")
284246

285-
# Button frame
286247
button_frame = create_styled_frame(page)
287248
button_frame.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
288249
button_layout = QtWidgets.QVBoxLayout(button_frame)
289-
# Button container
250+
290251
button_container = create_styled_frame(button_frame, min_size=(300, 0), style="background-color: #ffffff; border-radius: 15px; padding: 20px;")
291252
button_container_layout = QtWidgets.QVBoxLayout(button_container)
292253
button_container_layout.setSpacing(15)
293-
# Buttons
294-
add_button = create_styled_button(button_container, "Add Employee")
295-
update_employee = create_styled_button(button_container, "Update Employee")
296-
employee_list = create_styled_button(button_container, "Employee List")
297-
total_money = create_styled_button(button_container, "Total Money")
298-
back_to_home = create_styled_button(button_container, "Back")
299-
button_container_layout.addWidget(add_button)
300-
button_container_layout.addWidget(update_employee)
301-
button_container_layout.addWidget(employee_list)
302-
button_container_layout.addWidget(total_money)
303-
button_container_layout.addWidget(back_to_home)
254+
255+
# Define button labels
256+
button_labels = ["Add Employee", "Update Employee", "Employee List", "Total Money", "Back"]
257+
buttons = []
258+
259+
for label in button_labels:
260+
btn = create_styled_button(button_container, label)
261+
button_container_layout.addWidget(btn)
262+
buttons.append(btn)
263+
304264
button_layout.addWidget(button_container, 0, QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
305265
main_layout.addWidget(button_frame)
306-
# Connect button signals
307-
# add_button.clicked.connect(on_add_employee_clicked)
308-
# update_employee.clicked.connect(on_update_employee_clicked)
309-
# employee_list.clicked.connect(on_employee_list_clicked)
310-
# total_money.clicked.connect(on_total_money_clicked)
311-
# back_to_home.clicked.connect(on_back_to_home_clicked)
312-
return page,add_button,update_employee,employee_list,total_money,back_to_home
313-
314-
def create_add_employe_page(parent ,title, name_field_text="Name :", password_field_text="Password :",position_fielld_text="Position :",salary_field_text="Salary :",submit_text="Submit",):
315-
"""Create a login page with a title, name and password fields, and a submit button."""
316-
page = QtWidgets.QWidget(parent)
317-
main_layout = QtWidgets.QVBoxLayout(page)
318-
319-
# Header frame with title
320-
header_frame = create_styled_frame(page, style="background-color: #ffffff; border-radius: 10px; padding: 10px;")
321-
header_layout = QtWidgets.QVBoxLayout(header_frame)
322-
title_label = create_styled_label(header_frame, title, font_size=30)
323-
header_layout.addWidget(title_label, 0, QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop)
324-
main_layout.addWidget(header_frame, 0, QtCore.Qt.AlignTop)
266+
267+
return page, *buttons # Unpack as add_button, update_employee, etc.
268+
325269

326-
# Content frame
270+
def create_add_employee_page(parent, title, submit_text="Submit"):
271+
page, main_layout = create_page_with_header(parent, title)
272+
327273
content_frame = create_styled_frame(page)
328274
content_frame.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
329275
content_layout = QtWidgets.QVBoxLayout(content_frame)
330-
331-
# Form frame
276+
332277
form_frame = create_styled_frame(content_frame, min_size=(340, 200), style="background-color: #ffffff; border-radius: 15px; padding: 10px;")
333278
form_layout = QtWidgets.QVBoxLayout(form_frame)
334279
form_layout.setSpacing(20)
335-
336-
# Input fields
337-
name_frame, name_edit = create_input_field(form_frame, name_field_text)
338-
password_frame, password_edit = create_input_field(form_frame, password_field_text)
339-
salary_frame, salary_edit = create_input_field(form_frame, salary_field_text)
340-
position_frame, position_edit = create_input_field(form_frame, position_fielld_text)
341-
280+
281+
# Define input fields
282+
fields = ["Name :", "Password :", "Salary :", "Position :"]
283+
edits = []
284+
285+
for field in fields:
286+
field_frame, field_edit = create_input_field(form_frame, field)
287+
form_layout.addWidget(field_frame)
288+
edits.append(field_edit)
289+
342290
# Submit button
343291
button_frame = create_styled_frame(form_frame, style="padding: 7px;")
344292
button_layout = QtWidgets.QVBoxLayout(button_frame)
345-
button_layout.setSpacing(60)
346293
submit_button = create_styled_button(button_frame, submit_text, min_size=(150, 0))
347294
button_layout.addWidget(submit_button, 0, QtCore.Qt.AlignHCenter)
348-
349-
form_layout.addWidget(name_frame)
350-
form_layout.addWidget(password_frame)
351-
form_layout.addWidget(salary_frame)
352-
form_layout.addWidget(position_frame)
295+
353296
form_layout.addWidget(button_frame)
354-
355297
content_layout.addWidget(form_frame, 0, QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
356298
main_layout.addWidget(content_frame)
357-
358-
return page, name_edit, password_edit, salary_edit, position_edit,submit_button
299+
300+
return page, *edits, submit_button # Unpack as name_edit, password_edit, etc.
301+
359302
def setup_main_window(main_window):
360303
"""Set up the main window with a stacked widget containing home, admin, and employee pages."""
361304
main_window.setObjectName("MainWindow")
@@ -383,6 +326,7 @@ def admin_login_menu_page(name, password):
383326
stacked_widget.setCurrentIndex(3)
384327
else:
385328
print("Invalid admin credentials")
329+
show_popup_message(stacked_widget,"Invalid admin credentials",0)
386330

387331
def add_employee_form_submit(name, password, salary, position):
388332
if (
@@ -392,34 +336,74 @@ def add_employee_form_submit(name, password, salary, position):
392336
and len(position) != 0
393337
):
394338
backend.create_employee(name, password, salary, position)
395-
pop_up_message_with_only_ok(stacked_widget,"Employee added successfully",3)
339+
show_popup_message(stacked_widget,"Employee added successfully",3,False)
396340

397341
else:
398342
print("Please fill in all fields")
399-
pop_up_message(stacked_widget,"Please fill in all fields",3)
343+
show_popup_message(stacked_widget,"Please fill in all fields",3)
400344

401345

402-
home_page = create_home_page(stacked_widget, switch_to_admin, switch_to_employee, exit_app)
403-
admin_page, admin_name, admin_password, admin_submit = create_login_page(stacked_widget, "Admin Login")
346+
# Create Home Page
347+
home_page = create_home_page(
348+
stacked_widget,
349+
switch_to_admin,
350+
switch_to_employee,
351+
exit_app
352+
)
353+
354+
# Create Admin Login Page
355+
admin_page, admin_name, admin_password, admin_submit = create_login_page(
356+
stacked_widget,
357+
title="Admin Login"
358+
)
359+
admin_password.setEchoMode(QtWidgets.QLineEdit.Password)
360+
admin_name.setFont(QtGui.QFont("Arial", 10))
361+
admin_password.setFont(QtGui.QFont("Arial", 10))
362+
admin_name.setPlaceholderText("Enter your name")
363+
admin_password.setPlaceholderText("Enter your password")
364+
404365
admin_submit.clicked.connect(
405-
lambda: admin_login_menu_page(admin_name.text(), admin_password.text())
366+
lambda: admin_login_menu_page(
367+
admin_name.text(),
368+
admin_password.text()
369+
)
370+
)
371+
372+
# Create Admin Menu Page
373+
admin_menu_page, add_button, update_button, list_button, money_button, back_button = create_admin_menu_page(
374+
stacked_widget
406375
)
407-
admin_menu_page,add_button,update_employee,employee_list,total_money,back_to_home = create_admin_menu_page(stacked_widget)
408-
add_button.clicked.connect(lambda:stacked_widget.setCurrentIndex(4))
409-
# create employee page
410-
add_employe_page , new_employee_name, new_employee_password, new_employe_salary, new_employe_position, new_employee_submit = create_add_employe_page(stacked_widget, "Add Employee")
411-
new_employee_submit.clicked.connect(
412-
lambda: add_employee_form_submit(new_employee_name.text(), new_employee_password.text(), new_employe_salary.text(), new_employe_position.text())
376+
377+
add_button.clicked.connect(lambda: stacked_widget.setCurrentIndex(4))
378+
379+
# Create Add Employee Page
380+
add_employee_page, emp_name, emp_password, emp_salary, emp_position, emp_submit = create_add_employee_page(
381+
stacked_widget,
382+
title="Add Employee"
383+
)
384+
385+
emp_submit.clicked.connect(
386+
lambda: add_employee_form_submit(
387+
emp_name.text(),
388+
emp_password.text(),
389+
emp_salary.text(),
390+
emp_position.text()
391+
)
392+
)
393+
394+
# Create Employee Login Page
395+
employee_page, employee_name, employee_password, employee_submit = create_login_page(
396+
stacked_widget,
397+
title="Employee Login"
413398
)
414-
employee_page, employee_name, employee_password, employee_submit = create_login_page(stacked_widget, "Employee Login")
415399

416400

417401
# Add pages to stacked widget
418402
stacked_widget.addWidget(home_page)#1
419403
stacked_widget.addWidget(admin_page)#2
420404
stacked_widget.addWidget(employee_page)#3
421405
stacked_widget.addWidget(admin_menu_page)#4
422-
stacked_widget.addWidget(add_employe_page)#5
406+
stacked_widget.addWidget(add_employee_page)#5
423407

424408
main_layout.addWidget(stacked_widget)
425409
main_window.setCentralWidget(central_widget)

0 commit comments

Comments
 (0)