-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
299 lines (234 loc) · 9.28 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
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QGridLayout, \
QLineEdit, QPushButton, QMainWindow, QTableWidget, QTableWidgetItem, QDialog, \
QVBoxLayout, QComboBox, QToolBar, QStatusBar, QMessageBox
from PyQt6.QtGui import QAction, QIcon
import sys
import sqlite3
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Student Management System")
self.setMinimumSize(800, 600)
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("About", self)
help_menu_item.addAction(about_action)
about_action.setMenuRole(QAction.MenuRole.NoRole)
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.table.verticalHeader().setVisible(False)
self.setCentralWidget(self.table)
# Create toolbar and toolbar elements
toolbar = QToolBar()
toolbar.setMovable(True)
self.addToolBar(toolbar)
toolbar.addAction(add_student_action)
toolbar.addAction(search_action)
# Create status bar and add status bar elemenents
self.statusbar = QStatusBar()
self.setStatusBar(self.statusbar)
# Detect a cell click
self.table.cellClicked.connect(self.cell_clicked)
def cell_clicked(self):
edit_button = QPushButton("Edit Button")
edit_button.clicked.connect(self.edit)
delete_button = QPushButton("Delete Button")
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()
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 was created during the course"
The Python Mega Course"
Feel free to modify and restore
"""
self.setText(content)
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()
# get id from selected row
self.student_id = main_window.table.item(index, 0).text()
# Add student name widget
self.student_name = QLineEdit(student_name)
self.student_name.setPlaceholderText("Name")
layout.addWidget(self.student_name)
# Add combo box of courses
course_name = main_window.table.item(index, 2).text()
self.course_name = QComboBox()
courses = ["Biology", "Math", "Astronomy", "Physics"]
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 a submit button
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()
# Refresh the table
main_window.load_data()
class DeleteDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Delete Student Data")
layout = QGridLayout()
confirmation = QLabel("Are you sure you want to delete?")
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 and student_id
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()
# Refresh the table
main_window.load_data()
self.close()
confirmation_widget = QMessageBox()
confirmation_widget.setWindowTitle("Success")
confirmation_widget.setText("The record was deleted succesfully")
confirmation_widget.exec()
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", "Math", "Astronomy", "Physics"]
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 a submit button
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()
class SearchDialog(QDialog):
def __init__(self):
super().__init__()
# Set window title and size
self.setWindowTitle("Search Student")
self.setFixedWidth(300)
self.setFixedHeight(300)
# Create layout and input widget
layout = QVBoxLayout()
self.student_name = QLineEdit()
self.student_name.setPlaceholderText("Name")
layout.addWidget(self.student_name)
# Create button
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,))
row = list(result)[0]
print(row)
items = main_window.table.findItems("John Smith", 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())