-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
322 lines (257 loc) · 10.3 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
from PyQt6.QtWidgets import QApplication, QLabel, QGridLayout, \
QLineEdit, QPushButton, QMainWindow, QTableWidget, QTableWidgetItem, \
QDialog, QVBoxLayout, QComboBox, QToolBar, QStatusBar, QMessageBox
from PyQt6.QtGui import QAction, QIcon
from PyQt6.QtCore import Qt
import sys
import mysql.connector
class DatabaseConnection:
def __init__(self, host="localhost", user="root", password="pythoncourse", database="school"):
self.host = host
self.user = user
self.password = password
self.database = database
def connect(self):
connection = mysql.connector.connect(host=self.host, user=self.user,
password=self.password,
database=self.database)
return connection
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Student Management System")
self.setMinimumSize(800, 600)
# Create the File, Help, Edit buttons
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)
add_student_action.triggered.connect(self.insert)
file_menu_item.addAction(add_student_action)
about_action = QAction(QIcon("icons/about.jpg"), "About", self)
help_menu_item.addAction(about_action)
about_action.triggered.connect(self.about)
# If the help button does not show up on Mac, use the following code
# about_action.setMenuRole(QAction.MenuRole.NoRole)
search_action = QAction(QIcon("icons/search.png"), "Search", self)
search_action.triggered.connect(self.search)
edit_menu_item.addAction(search_action)
# Create toolbar and add elements
toolbar = QToolBar()
toolbar.setMovable(True)
self.addToolBar(toolbar)
toolbar.addAction(add_student_action)
toolbar.addAction(search_action)
# Add the table
self.table = QTableWidget()
self.table.setColumnCount(4)
self.table.setHorizontalHeaderLabels(
("Id", "Name", "Course", "Mobile"))
self.table.verticalHeader().setVisible(False)
self.setCentralWidget(self.table)
# Add a status bar and status bar elements
self.statusbar = QStatusBar()
self.setStatusBar(self.statusbar)
# Detect a cell click
self.table.cellClicked.connect(self.cell_clicked)
def cell_clicked(self):
# Add the edit and delete buttons
edit_button = QPushButton("Edit Record")
edit_button.clicked.connect(self.edit)
delete_button = QPushButton("Delete Record")
delete_button.clicked.connect(self.delete)
# Remove any buttons on the status bar
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):
# Add the data to the window as a table
connection = DatabaseConnection().connect()
cursor = connection.cursor()
cursor.execute("SELECT * FROM students")
result = cursor.fetchall()
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)))
cursor.close()
connection.close()
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 made by Subhojit Guin.
Feel free to modify and reuse it.
"""
self.setText(content)
class InsertDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Insert Student Data")
self.setFixedWidth(300)
self.setFixedHeight(300)
layout = QVBoxLayout()
# Add student name widget
self.student_name = QLineEdit()
self.student_name.setPlaceholderText("Name")
layout.addWidget(self.student_name)
# Add combo box of courses
self.course_name = QComboBox()
courses = ["Biology", "Physics", "Astronomy", "Maths"]
self.course_name.addItems(courses)
layout.addWidget(self.course_name)
# Add mobile widget
self.mobile = QLineEdit()
self.mobile.setPlaceholderText("Mobile")
layout.addWidget(self.mobile)
# Add register button
button = QPushButton("Register")
button.clicked.connect(self.add_student)
layout.addWidget(button)
self.setLayout(layout)
def add_student(self):
# Add the student record to the database
name = self.student_name.text()
course = self.course_name.itemText(self.course_name.currentIndex())
mobile = self.mobile.text()
connection = DatabaseConnection().connect()
cursor = connection.cursor()
cursor.execute("INSERT INTO students (name, course, mobile) VALUES (%s,%s,%s)",
(name, course, mobile))
connection.commit()
cursor.close()
connection.close()
main_window.load_data()
class SearchDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Search Student")
self.setFixedWidth(300)
self.setFixedHeight(300)
layout = QVBoxLayout()
# Add student name widget
self.student_name = QLineEdit()
self.student_name.setPlaceholderText("Name")
layout.addWidget(self.student_name)
# Add search button
button = QPushButton("Search")
button.clicked.connect(self.search)
layout.addWidget(button)
self.setLayout(layout)
def search(self):
name = self.student_name.text()
main_window.table.clearSelection()
connection = DatabaseConnection().connect()
cursor = connection.cursor()
cursor.execute("SELECT * FROM students WHERE name=%s", (name,))
result = cursor.fetchall()
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()
class EditDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Update Student Data")
self.setFixedWidth(300)
self.setFixedHeight(300)
layout = QVBoxLayout()
# Get student name from selected row
index = main_window.table.currentRow()
student_name = main_window.table.item(index, 1).text()
# Add student name widget
self.student_name = QLineEdit(student_name)
self.student_name.setPlaceholderText("Name")
layout.addWidget(self.student_name)
# Get student id from selected row
self.student_id = main_window.table.item(index, 0).text()
# Add combo box of courses
course_name = main_window.table.item(index, 2).text()
self.course_name = QComboBox()
courses = ["Biology", "Physics", "Astronomy", "Maths"]
self.course_name.addItems(courses)
self.course_name.setCurrentText(course_name)
layout.addWidget(self.course_name)
# Add mobile widget
mobile = main_window.table.item(index, 3).text()
self.mobile = QLineEdit(mobile)
self.mobile.setPlaceholderText("Mobile")
layout.addWidget(self.mobile)
# Add register button
button = QPushButton("Update")
button.clicked.connect(self.update_student)
layout.addWidget(button)
self.setLayout(layout)
def update_student(self):
connection = DatabaseConnection().connect()
cursor = connection.cursor()
cursor.execute("UPDATE students SET name = %s, course = %s, mobile = %s WHERE id = %s",
(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()
class DeleteDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Delete Student Data")
layout = QGridLayout()
# Add the confirmation message, and yes and no buttons
confirmation = QLabel("Are you sure you want to permanently delete it?")
yes = QPushButton("Yes")
no = QPushButton("No")
layout.addWidget(confirmation, 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):
# Get selected row index and student id
index = main_window.table.currentRow()
student_id = main_window.table.item(index, 0).text()
connection = DatabaseConnection().connect()
cursor = connection.cursor()
cursor.execute("DELETE FROM students WHERE id = %s", (student_id,))
connection.commit()
cursor.close()
connection.close()
main_window.load_data()
self.close()
# Display a confirmation after deletion
confirmation_widget = QMessageBox()
confirmation_widget.setWindowTitle("Success")
confirmation_widget.setText("The record was deleted successfully!")
confirmation_widget.exec()
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
main_window.load_data()
app.exit(app.exec())