-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
301 lines (225 loc) · 9.09 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
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QAction, QIcon
from PyQt6.QtWidgets import (QApplication, QVBoxLayout,
QLabel, QGridLayout,
QLineEdit, QPushButton, QMainWindow, QTableWidget, QTableWidgetItem, QDialog, QToolBar,
QComboBox, QStatusBar, QMessageBox)
import sys
import sqlite3
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("School Management System")
self.setMinimumSize(500, 400)
file_menu_item = self.menuBar().addMenu("&File")
help_menu_item = self.menuBar().addMenu("&Help")
edit_menu_item = self.menuBar().addMenu("&Edit")
add_student_action = QAction(QIcon("icons/add.png"), "Add Student", self)
file_menu_item.addAction(add_student_action)
add_student_action.triggered.connect(self.insert)
about_action = QAction("About", self)
help_menu_item.addAction(about_action)
about_action.triggered.connect(self.about)
search_action = QAction(QIcon("icons/search.png"), "Search", self)
edit_menu_item.addAction(search_action)
search_action.triggered.connect(self.search)
self.table = QTableWidget()
self.table.setColumnCount(4)
self.table.setHorizontalHeaderLabels(("id", "name", "course", "mobile"))
self.setCentralWidget(self.table)
# Toolbar and Toolbar Elements
toolbar = QToolBar()
toolbar.setMovable(True)
self.addToolBar(toolbar)
toolbar.addAction(add_student_action)
toolbar.addAction(search_action)
# Status Bar
self.statusbar = QStatusBar()
self.setStatusBar(self.statusbar)
# Detect Cell Click
self.table.cellClicked.connect(self.cell_clicked)
def cell_clicked(self):
edit_button = QPushButton("Edit Record")
edit_button.clicked.connect(self.edit)
delete_button = QPushButton("Delete Record")
delete_button.clicked.connect(self.delete)
children = self.findChildren(QPushButton)
if children:
for child in children:
self.statusbar.removeWidget(child)
self.statusbar.addWidget(edit_button)
self.statusbar.addWidget(delete_button)
def load_Data(self):
connection = sqlite3.connect("database.db")
result = connection.execute("SELECT * FROM Students")
self.table.setRowCount(0)
for row_number, row_data in enumerate(result):
self.table.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.table.setItem(row_number, column_number, QTableWidgetItem(str(data)))
connection.close()
pass
def insert(self):
dialog = InsertDialog()
dialog.exec()
def search(self):
dialog = SearchDialog()
dialog.exec()
def edit(self):
dialog = EditDialog()
dialog.exec()
def delete(self):
dialog = DeleteDialog()
dialog.exec()
def about(self):
dialog = AboutDialog()
dialog.exec()
class AboutDialog(QMessageBox):
def __init__(self):
super().__init__()
self.setWindowTitle("About")
content = """
This app is a school management system where it keeps the records
of students along with their course and mobile number.
"""
self.setText(content)
class EditDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Update Student")
self.setFixedWidth(300)
self.setFixedHeight(300)
layout = QVBoxLayout()
# Get student name from selected row
index = main_window.table.currentRow()
self.student_id = main_window.table.item(index, 0).text()
student_name = main_window.table.item(index, 1).text()
self.student_name = QLineEdit(student_name)
self.student_name.setPlaceholderText("Name")
layout.addWidget(self.student_name)
course_name = main_window.table.item(index, 2).text()
self.course_name = QComboBox()
courses = ["Biology", "Math", "Physics", "Chemistry"]
self.course_name.addItems(courses)
self.course_name.setCurrentText(course_name)
layout.addWidget(self.course_name)
mobile = main_window.table.item(index, 3).text()
self.mobile = QLineEdit(mobile)
self.mobile.setPlaceholderText("Mobile")
layout.addWidget(self.mobile)
button = QPushButton("Update")
button.clicked.connect(self.update_student)
layout.addWidget(button)
self.setLayout(layout)
def update_student(self):
connection = sqlite3.connect("database.db")
cursor = connection.cursor()
cursor.execute("UPDATE students SET name = ?, course = ?, mobile = ? WHERE id = ?", (
self.student_name.text(), self.course_name.itemText(self.course_name.currentIndex()), self.mobile.text(),
self.student_id))
connection.commit()
cursor.close()
connection.close()
main_window.load_Data()
self.close()
conformation_msg = QMessageBox()
conformation_msg.setWindowTitle("Success")
conformation_msg.setText("The student record was updated successfully")
conformation_msg.exec()
class DeleteDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Delete Student Record")
layout = QGridLayout()
conformation = QLabel("Are you sure ? Want to delete ?")
yes = QPushButton("Yes")
no = QPushButton("No")
layout.addWidget(conformation, 0, 0, 1, 2)
layout.addWidget(yes, 1, 0)
layout.addWidget(no, 1, 1)
self.setLayout(layout)
yes.clicked.connect(self.delete_student)
def delete_student(self):
index = main_window.table.currentRow()
student_id = main_window.table.item(index, 0).text()
connection = sqlite3.connect("database.db")
cursor = connection.cursor()
cursor.execute("DELETE from students WHERE id = ?", (student_id,))
connection.commit()
cursor.close()
connection.close()
main_window.load_Data()
self.close()
conformation_msg = QMessageBox()
conformation_msg.setWindowTitle("Success")
conformation_msg.setText("The student record was deleted successfully")
conformation_msg.exec()
class InsertDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Insert Student")
self.setFixedWidth(300)
self.setFixedHeight(300)
layout = QVBoxLayout()
self.student_name = QLineEdit()
self.student_name.setPlaceholderText("Name")
layout.addWidget(self.student_name)
self.course_name = QComboBox()
courses = ["Biology", "Math", "Physics", "Chemistry"]
self.course_name.addItems(courses)
layout.addWidget(self.course_name)
self.mobile = QLineEdit()
self.mobile.setPlaceholderText("Mobile")
layout.addWidget(self.mobile)
button = QPushButton("Register")
button.clicked.connect(self.add_student)
layout.addWidget(button)
self.setLayout(layout)
def add_student(self):
name = self.student_name.text()
course = self.course_name.itemText(self.course_name.currentIndex())
mobile = self.mobile.text()
connection = sqlite3.connect("database.db")
cursor = connection.cursor()
cursor.execute("INSERT INTO students (name, course, mobile) VALUES (?, ?, ?)", (name, course, mobile))
connection.commit()
cursor.close()
connection.close()
main_window.load_Data()
self.close()
conformation_msg = QMessageBox()
conformation_msg.setWindowTitle("Success")
conformation_msg.setText("The student record was added successfully")
conformation_msg.exec()
class SearchDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Search Student")
self.setFixedWidth(300)
self.setFixedHeight(300)
layout = QVBoxLayout()
self.student_name = QLineEdit()
self.student_name.setPlaceholderText("Name")
layout.addWidget(self.student_name)
button = QPushButton("Search")
button.clicked.connect(self.search)
layout.addWidget(button)
self.setLayout(layout)
def search(self):
name = self.student_name.text()
connection = sqlite3.connect("database.db")
cursor = connection.cursor()
result = cursor.execute("SELECT * FROM students WHERE name = ?", (name,))
rows = list(result)
print(rows)
items = main_window.table.findItems(name, Qt.MatchFlag.MatchFixedString)
for item in items:
print(item)
main_window.table.item(item.row(), 1).setSelected(True)
cursor.close()
connection.close()
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
main_window.load_Data()
sys.exit(app.exec())