-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
290 lines (252 loc) · 9.22 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
import hashlib
from PySide2.QtSql import *
from PySide2.QtWidgets import *
from PySide2.QtCore import *
import ChangeAnyPwd
import Login
import Regist
import StuInformation
import Student
import SubmitStudentInformation
import Teacher
import UserManager
import os
# 数据库
db = QSqlDatabase.addDatabase('QSQLITE')
# 登入界面
login_dialog = Login.Ui_Dialog()
# 注册界面
regist_dialog = Regist.Ui_Dialog()
# 学生主界面
student_dialog = Student.Ui_Dialog()
# 学生信息修改界面
stuinfor = StuInformation.Ui_Dialog()
# 教师主界面
teacher_dialog = Teacher.Ui_Dialog()
# 修改密码
changeAnyPwd_dialog = ChangeAnyPwd.Ui_Dialog()
# 提交学生信息
submitStudent_dialog = SubmitStudentInformation.Ui_Dialog()
stuinforModel = QSqlTableModel()
# 窗口
window = ''
# 当前用户名
USER = ''
# 用于编码密码,便于将密码以sha256的方式存在数据库中
def encodePWD(password):
h = hashlib.sha256()
h.update(bytes(password, encoding='utf-8'))
return h.hexdigest()
# 将学生信息储存,并退出返回至学生主界面
def stuinfoSaveAndExit():
global stuinfor
global USER
sql = "INSERT INTO STUDENT VALUES ('temp','" + stuinfor.textName.text() + "','" + stuinfor.textID.text() + "','" + stuinfor.textPeople.text() + "','" + stuinfor.textIncome.text() + "','" + stuinfor.textCostForFood.text() + "','" + stuinfor.textCostForOther.text() + "')"
model = QSqlQueryModel()
model.setQuery("DELETE FROM STUDENT WHERE USERNAME = 'temp'")
model.setQuery(sql)
if model.lastError().isValid():
stuinfor.labelError.setText(
'<html><head/><body><p><span style=" color:#ff0000;">您输入的信息有误</span></p></body></html>')
return
else:
model.setQuery("DELETE FROM STUDENT WHERE USERNAME = '" + USER + "'")
model.setQuery(
"INSERT INTO STUDENT VALUES ('" + USER + "','" + stuinfor.textName.text() + "','" + stuinfor.textID.text() + "','" + stuinfor.textPeople.text() + "','" + stuinfor.textIncome.text() + "','" + stuinfor.textCostForFood.text() + "','" + stuinfor.textCostForOther.text() + "')")
StudentSystem()
def submit(user):
model = QSqlQueryModel()
model.setQuery('INSERT INTO SUBMIT VALUES("' + user + '" , 1)')
def submitYes():
global USER
global submitStudent_dialog
submit(USER)
submitStudent_dialog.label_2.setVisible(True)
def submitStudentInformation():
global window
global submitStudent_dialog
window = QDialog()
submitStudent_dialog.setupUi(window)
window.show()
submitStudent_dialog.label_2.setVisible(False)
submitStudent_dialog.buttonYes.clicked.connect(submitYes)
submitStudent_dialog.buttonCancel.clicked.connect(StudentSystem)
# 学生信息编辑
def StuInforEdit():
global USER
global window
global stuinfor
window = QDialog()
stuinfor.setupUi(window)
window.show()
# 将保存按钮和保存并退出的槽函数绑定
stuinfor.buttonSave.clicked.connect(stuinfoSaveAndExit)
query = QSqlQueryModel()
query.setQuery("SELECT * FROM STUDENT WHERE USERNAME = '" + USER + "'")
# 将数据库中的学生信息导入至学生信息表
if query.rowCount() > 0:
stuinfor.textID.setText(query.data(query.index(0, 2)))
stuinfor.textName.setText(query.data(query.index(0, 1)))
stuinfor.textPeople.setText(str(query.data(query.index(0, 3))))
stuinfor.textIncome.setText(str(query.data(query.index(0, 4))))
stuinfor.textCostForFood.setText(str(query.data(query.index(0, 5))))
stuinfor.textCostForOther.setText(str(query.data(query.index(0, 6))))
model = QSqlQueryModel()
model.setQuery('SELECT * FROM SUBMIT WHERE USERNAME = "' + USER + '"')
if model.rowCount() > 0:
mod = model.data(model.index(0, 1))
print(mod)
if mod > 0:
stuinfor.textID.setFocusPolicy(Qt.NoFocus)
stuinfor.textName.setFocusPolicy(Qt.NoFocus)
stuinfor.textPeople.setFocusPolicy(Qt.NoFocus)
stuinfor.textIncome.setFocusPolicy(Qt.NoFocus)
stuinfor.textCostForFood.setFocusPolicy(Qt.NoFocus)
stuinfor.textCostForOther.setFocusPolicy(Qt.NoFocus)
stuinfor.labelError.setText(
'<html><head/><body><p><span style=" color:#ff0000;">信息已提交,不可更改</span></p></body></html>'
)
# 启动学生信息系统
def StudentSystem():
global student_dialog
global USER
global window
window = QDialog()
student_dialog.setupUi(window)
window.show()
# 显示欢迎词
student_dialog.labelUsername.setText(USER + ",您好!")
# 将信号和槽函数绑定
student_dialog.buttonEdit.clicked.connect(StuInforEdit)
student_dialog.buttonSubmit.clicked.connect(submitStudentInformation)
# 用户管理系统
def usermanager():
global window
window = QDialog()
userMgr = UserManager.Ui_Dialog()
userMgr.setupUi(window)
view = userMgr.tableUser
model = QSqlTableModel()
view.setModel(model)
model.setTable('USER')
model.select()
model.setHeaderData(0, Qt.Horizontal, '用户名')
model.setHeaderData(1, Qt.Horizontal, '密码')
model.setHeaderData(2, Qt.Horizontal, '用户权限')
window.show()
# 启动教师信息系统
def teacherSystem():
global teacher_dialog
global window
window = QDialog()
teacher_dialog.setupUi(window)
window.show()
teacher_dialog.buttonUserManager.clicked.connect(usermanager)
teacher_dialog.changpwd.clicked.connect(changeAnyPwd)
def changeapwd():
global changeAnyPwd_dialog
username = changeAnyPwd_dialog.textUsername.text()
password = changeAnyPwd_dialog.textPassword.text()
model = QSqlQueryModel()
model.setQuery("SELECT * FROM USER WHERE USERNAME = '" + username + "'")
# print("SELECT * FROM USER WHERE USERNAME = '" + username + "'")
if model.rowCount() > 0:
pwd = encodePWD(password)
model.setQuery('UPDATE USER SET PASSWORD = "' + pwd + '" WHERE USERNAME = "' + username + '"')
# print('UPDATE USER SET PASSWORD = "' + pwd + '" WHERE USERNAME = "' + username + '"')
changeAnyPwd_dialog.labelError.setText('密码修改成功')
else:
print("here")
changeAnyPwd_dialog.labelError.setText('用户名不存在')
def changeAnyPwd():
global window
global changeAnyPwd_dialog
window = QDialog()
changeAnyPwd_dialog.setupUi(window)
window.show()
changeAnyPwd_dialog.buttonChange.clicked.connect(changeapwd)
changeAnyPwd_dialog.buttonBack.clicked.connect(teacherSystem)
# 尝试登入账户
def loginAUser():
global login_dialog
global USER
# 获取用户名及密码
username = login_dialog.textUsername.text()
password = login_dialog.textPassword.text()
# 计算密码的hash
pwd = encodePWD(password)
model = QSqlQueryModel()
# 获取数据库中的密码并进行比对
model.setQuery("SELECT * FROM USER WHERE USERNAME = '" + username + "'")
if model.lastError().isValid():
login_dialog.lableError.setText("用户名不存在")
else:
realPwd = model.data(model.index(0, 1))
if realPwd == pwd:
mod = model.data(model.index(0, 2))
USER = username
if mod == 1:
StudentSystem()
elif mod == 2:
teacherSystem()
else:
login_dialog.lableError.setText("用户名或密码错误")
# 连接数据库
def connectdb():
global db
db.setDatabaseName('student.db')
# 打开连接
db.open()
# 尝试注册账户
def registAUser():
model = QSqlQueryModel()
username = regist_dialog.testUsername.text()
password = regist_dialog.testPassword.text()
password1 = regist_dialog.textPassword1.text()
# 比较两次输入的密码是否一致
if password1 != password:
regist_dialog.labelError.setText("两次输入密码不一致!")
return
# 查询数据库中是否存在同名用户
model.setQuery("SELECT * FROM USER WHERE USERNAME = '" + username + "'")
# debug
# print(username)
# print("SELECT * FROM USER WHERE USERNAME = '" + username + "'")
# print(model.rowCount())
if model.rowCount() == 0:
pwd = encodePWD(password)
model.setQuery("INSERT INTO USER VALUES (" + "'" + username + "'," + "'" + pwd + "'," + "1)")
regist_dialog.labelError.setText("注册成功")
else:
regist_dialog.labelError.setText("用户名已存在")
# 启动注册系统
def registSystem():
global regist_dialog
global window
window = QDialog()
regist_dialog.setupUi(window)
window.show()
regist_dialog.buttonSubmit.clicked.connect(registAUser)
# 启动登入系统
def loginSystem():
global window
global login_dialog
window = QDialog()
login_dialog.setupUi(window)
window.show()
# 执行登入操作
login_dialog.buttonLogin.clicked.connect(loginAUser)
login_dialog.textPassword.setEchoMode(QLineEdit.Password)
login_dialog.buttonRegist.clicked.connect(registSystem)
# main函数
def main():
# MACOS BIGSUR兼容
os.environ['QT_MAC_WANTS_LAYER'] = '1'
# QT环境初始化
connectdb()
app = QApplication()
loginSystem()
app.exec_()
# 程序入口
if __name__ == '__main__':
main()