-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent.py
More file actions
454 lines (404 loc) · 18.4 KB
/
student.py
File metadata and controls
454 lines (404 loc) · 18.4 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import tkinter
from tkinter import ttk
import pymysql
from utils import *
RADIO = 0.1
WINDOW_SIZE = '800x500+200+100'
TEXT_INDENT = 60
TEXT_LINE_SPACE = 30
class menu:
def __init__(self, StudentNum):
self.studentNum = StudentNum
self.root = tkinter.Tk()
self.root.geometry(WINDOW_SIZE)
self.root.title('学生')
self.root.resizable(False, False)
# 子区域
self.info = tkinter.Frame(self.root)
self.choose = tkinter.Frame(self.root)
self.drop = tkinter.Frame(self.root)
self.grade = tkinter.Frame(self.root)
# 菜单栏
self.menu = tkinter.Frame(self.root)
self.stu_info = tkinter.Button(self.menu, text='个人信息', command=self.stu_info)
self.stu_choose = tkinter.Button(self.menu, text='选课', command=self.stu_choose)
self.stu_drop = tkinter.Button(self.menu, text='退课', command=self.stu_drop)
self.stu_grade = tkinter.Button(self.menu, text='课程成绩', command=self.stu_grade)
# 控件布局
self.initialize()
def initialize(self):
self.menu.place(relheight=1, relwidth=RADIO)
self.stu_info.place(rely=0.0, relheight=0.25, relwidth=1)
self.stu_choose.place(rely=0.25, relheight=0.25, relwidth=1)
self.stu_drop.place(rely=0.5, relheight=0.25, relwidth=1)
self.stu_grade.place(rely=0.75, relheight=0.25, relwidth=1)
def stu_info(self):
self.info.place_forget()
self.choose.place_forget()
self.drop.place_forget()
self.grade.place_forget()
stuInfo(self.info, self.studentNum)
def stu_choose(self):
self.info.place_forget()
self.choose.place_forget()
self.drop.place_forget()
self.grade.place_forget()
stuChoose(self.choose, self.studentNum)
def stu_drop(self):
self.info.place_forget()
self.choose.place_forget()
self.drop.place_forget()
self.grade.place_forget()
stuDrop(self.drop, self.studentNum)
def stu_grade(self):
self.info.place_forget()
self.choose.place_forget()
self.drop.place_forget()
self.grade.place_forget()
stuGrade(self.grade, self.studentNum)
# 启动窗口
def start(self):
self.root.mainloop()
# 个人信息区域
class stuInfo:
def __init__(self, frame, studentNum):
self.frame = frame
self.studentNum = studentNum
# 个人信息
self.numLabel = tkinter.Label(self.frame, text='学号:')
self.nameLabel = tkinter.Label(self.frame, text='姓名:')
self.sexLabel = tkinter.Label(self.frame, text='性别:')
self.ageLabel = tkinter.Label(self.frame, text='年龄:')
self.pswdLabel = tkinter.Label(self.frame, text='密码:')
# 个人信息显示
self.num_Label = tkinter.Label(self.frame)
self.name_Label = tkinter.Label(self.frame)
self.sex_Label = tkinter.Label(self.frame)
self.age_Label = tkinter.Label(self.frame)
self.pswdVar = tkinter.StringVar()
self.pswd_Entry = tkinter.Entry(self.frame, textvariable=self.pswdVar)
# 修改密码
self.commitButton = tkinter.Button(self.frame, text='修改密码')
# 规则提示,修改密码提示信息是同一个标签
self.promptLabel = tkinter.Label(self.frame, text='密码不少于6位, 不能与原密码相同', font=("TkDefaultFont", 8))
# 控件布局
self.initialize()
def initialize(self):
self.frame.place(relx=RADIO, relheight=1, relwidth=1-RADIO)
# 放置不变的组件
self.numLabel.place(x=TEXT_INDENT, y=TEXT_LINE_SPACE * 0, anchor='ne')
self.nameLabel.place(x=TEXT_INDENT, y=TEXT_LINE_SPACE * 1, anchor='ne')
self.sexLabel.place(x=TEXT_INDENT, y=TEXT_LINE_SPACE * 2, anchor='ne')
self.ageLabel.place(x=TEXT_INDENT, y=TEXT_LINE_SPACE * 3, anchor='ne')
self.pswdLabel.place(x=TEXT_INDENT, y=TEXT_LINE_SPACE * 4, anchor='ne')
# 获取信息
info = self.get_student_info(self.studentNum)[0]
# 设置要显示的信息,及修改密码的函数
self.num_Label.configure(text=info[0])
self.name_Label.configure(text=info[1])
self.sex_Label.configure(text=info[2])
self.age_Label.configure(text=info[3])
self.pswd_Entry.insert(tkinter.INSERT, info[4])
self.commitButton.configure(command=lambda :self.change_pswd(info[4], self.pswd_Entry.get()))
# 放置组件,显示信息
self.num_Label.place(x=TEXT_INDENT, y=TEXT_LINE_SPACE * 0)
self.name_Label.place(x=TEXT_INDENT, y=TEXT_LINE_SPACE * 1)
self.sex_Label.place(x=TEXT_INDENT, y=TEXT_LINE_SPACE * 2)
self.age_Label.place(x=TEXT_INDENT, y=TEXT_LINE_SPACE * 3)
self.pswd_Entry.place(x=TEXT_INDENT, y=TEXT_LINE_SPACE * 4)
# 放置修改密码按钮,和密码提示
self.frame.update()
self.promptLabel.place(x=self.pswd_Entry.winfo_x(), y=self.pswd_Entry.winfo_y() + self.pswd_Entry.winfo_height() + 5)
self.commitButton.place(x=self.pswd_Entry.winfo_x() + self.pswd_Entry.winfo_width() + 5, y=self.pswd_Entry.winfo_y() - 5)
def change_pswd(self, origPswd, pswd):
pswd = pswd.strip()
if pswd == origPswd:
self.promptLabel.configure(text='密码不能与原密码相同!')
return
elif len(pswd) < 6:
self.promptLabel.configure(text='密码不能小于六位!')
return
else:
self.promptLabel.configure(text='密码修改成功!')
self.set_student_pswd(pswd)
return
def get_student_info(self, studentID):
db = connect()
cursor = db.cursor()
sql = f'select distinct * from tb_student where studentID = \'{studentID}\''
cursor.execute(sql)
result = cursor.fetchall()
db.close()
return result
def set_student_pswd(self, pswd):
db = connect()
cursor = db.cursor()
sql = f'UPDATE tb_student SET studentPswd= \'{pswd}\' WHERE studentID = \'{self.studentNum}\''
cursor.execute(sql)
try:
db.commit()
except Exception as e:
db.rollback()
db.close()
# 选课区域
class stuChoose:
def __init__(self, frame, studentNum):
self.frame = frame
self.studentNum = studentNum
# 信息放置处, 滚动条
self.resultScrollbar = tkinter.Scrollbar(self.frame)
self.resultTable = ttk.Treeview(self.frame,
height=10,
columns=['课程号', '课程名', '课序号', '学分'],
show='headings',
yscrollcommand=self.resultScrollbar.set)
# 选课按钮
self.chooseButton = tkinter.Button(self.frame, text='选课', command=self.chooseCourse)
# 提示信息
self.promptLabel = tkinter.Label(self.frame, text='一次选一门课,已选的课不出现在列表里')
# 选中的课程
self.selected = None
self.initialize()
# frame和button的布局
def initialize(self):
self.frame.place(relx=RADIO, relheight=1, relwidth=1-RADIO)
# 放置组件
self.frame.update()
self.frame.update()
self.resultTable.place(height=self.frame.winfo_height(),
relwidth=0.6)
self.frame.update()
self.promptLabel.place(x=self.resultTable.winfo_x() + self.resultTable.winfo_width() + 5,
y=self.resultTable.winfo_y() + self.resultTable.winfo_height(), anchor='sw')
self.chooseButton.place(x=self.resultTable.winfo_x() + self.resultTable.winfo_width() + 5,
y=self.resultTable.winfo_y() + self.resultTable.winfo_height() - 30, anchor='sw')
self.displayInfo()
# 显示选课信息
def displayInfo(self):
self.clear_course_info()
self.resultTable.heading('课程号', text='课程号', anchor='w')
self.resultTable.heading('课程名', text='课程名')
self.resultTable.heading('课序号', text='课序号')
self.resultTable.heading('学分', text='学分')
self.resultTable.column('课程号', width=100, minwidth=100)
self.resultTable.column('课程名', width=150, minwidth=150)
self.resultTable.column('课序号', width=50, minwidth=50)
self.resultTable.column('学分', width=100, minwidth=100)
info_valid = self.get_class_optional()
for x in info_valid:
self.resultTable.insert('', tkinter.END, values=x)
# print('以下是暂未安排老师的课程,不支持选择')
# info_invalid = self.get_invalid_class_optional()
# for x in info_invalid:
# self.resultTable.insert('', tkinter.END, values=x)
self.resultTable.bind('<ButtonRelease-1>', self.get_selected_item)
# 选课按钮
def chooseCourse(self):
if self.selected is None:
self.promptLabel.configure(text='还未选择课程!')
else:
if self.insert_student_course(self.studentNum, self.selected['values'][0], self.selected['values'][2]) == 0:
self.promptLabel.configure(text='选课成功!')
else:
self.promptLabel.configure(text='选课失败!')
# 选课完成后更新选课列表
self.displayInfo()
# 获得选中课程信息
def get_selected_item(self, event):
curItem = self.resultTable.focus()
self.selected = self.resultTable.item(curItem)
print(self.selected)
# 获取有效选课信息
def get_class_optional(self):
db = connect()
cursor = db.cursor()
sql = f'select C.courseID, C.courseName, C.courseNum, C.courseCredit ' \
f'from tb_course as C, tb_teacher_course as TC ' \
f'where C.courseID = TC.courseID and C.courseNum = TC.courseNum ' \
f'and C.courseID not in (select courseID from tb_student_course where studentID = \'{self.studentNum}\')'
cursor.execute(sql)
result = cursor.fetchall()
db.close()
return result
# 获取无效选课信息(没有分配老师的课程)
def get_invalid_class_optional(self):
db = connect()
cursor = db.cursor()
sql = f'select C.courseID, C.courseName, C.courseNum, C.courseCredit ' \
f'from tb_course as C ' \
f'where C.courseID not in \
(select C.courseID \
from tb_course as C, tb_teacher_course as TC, tb_student_course as SC \
where C.courseID = TC.courseID and C.courseNum = TC.courseNum and SC.studentID = \'{self.studentNum}\') '
cursor.execute(sql)
result = cursor.fetchall()
db.close()
return result
# 选课信息提交
def insert_student_course(self, studentID, courseID, courseNum):
db = connect()
cursor = db.cursor()
sql = f'INSERT INTO tb_student_course (studentID, courseID, courseNum, grade) VALUES (\'{studentID}\', \'{courseID}\', \'{courseNum}\', -1)'
cursor.execute(sql)
try:
db.commit()
db.close()
return 0
except:
db.rollback()
db.close()
return 1
# 选课信息清除
def clear_course_info(self):
for x in self.resultTable.get_children():
self.resultTable.delete(x)
# 退课区域
class stuDrop:
def __init__(self, frame, studentNum):
self.studentNum = studentNum
self.frame = frame
# 信息放置处, 滚动条
self.resultScrollbar = tkinter.Scrollbar(self.frame)
self.resultTable = ttk.Treeview(self.frame,
height=10,
columns=['课程号', '课程名', '课序号', '学分'],
show='headings',
yscrollcommand=self.resultScrollbar.set)
# 退课按钮
self.dropButton = tkinter.Button(self.frame, text='退课', command=self.dropCourse)
# 提示信息
self.promptLabel = tkinter.Label(self.frame, text='一次退一门课,已退的课不出现在列表里')
# 选中的课程
self.selected = None
self.initialize()
def initialize(self):
self.frame.place(relx=RADIO, relheight=1, relwidth=1-RADIO)
# 放置组件
# self.frame.update()
self.frame.update()
self.resultTable.place(height=self.frame.winfo_height(),
relwidth=0.6)
self.frame.update()
self.promptLabel.place(x=self.resultTable.winfo_x() + self.resultTable.winfo_width() + 5,
y=self.resultTable.winfo_y() + self.resultTable.winfo_height(), anchor='sw')
self.dropButton.place(x=self.resultTable.winfo_x() + self.resultTable.winfo_width() + 5,
y=self.resultTable.winfo_y() + self.resultTable.winfo_height() - 30, anchor='sw')
self.displayInfo()
# 显示选课信息
def displayInfo(self):
self.clear_course_info()
self.resultTable.heading('课程号', text='课程号', anchor='w')
self.resultTable.heading('课程名', text='课程名')
self.resultTable.heading('课序号', text='课序号')
self.resultTable.heading('学分', text='学分')
self.resultTable.column('课程号', width=50, minwidth=50)
self.resultTable.column('课程名', width=120, minwidth=120)
self.resultTable.column('课序号', width=50, minwidth=50)
self.resultTable.column('学分', width=50, minwidth=50)
info = self.get_class_optional()
for x in info:
self.resultTable.insert('', tkinter.END, values=x)
self.resultTable.bind('<ButtonRelease-1>', self.get_selected_item)
# 退课按钮
def dropCourse(self):
if self.selected is None:
self.promptLabel.configure(text='还未选择课程!')
else:
if self.drop_student_course(self.studentNum, self.selected['values'][0], self.selected['values'][2]) == 0:
self.promptLabel.configure(text='退课成功!')
else:
self.promptLabel.configure(text='退课失败!')
# 选课完成后更新选课列表
self.displayInfo()
# 获得选中课程信息
def get_selected_item(self, event):
curItem = self.resultTable.focus()
self.selected = self.resultTable.item(curItem)
print(self.selected)
# 获取已选信息
def get_class_optional(self):
db = connect()
cursor = db.cursor()
sql = f'select C.courseID, C.courseName, C.courseNum, C.courseCredit ' \
f'from tb_course as C, tb_student_course as SC ' \
f'where SC.studentID = \'{self.studentNum}\' and C.courseID = SC.courseID and C.courseNum = SC.courseNum'
cursor.execute(sql)
result = cursor.fetchall()
db.close()
return result
# 退课信息提交
def drop_student_course(self, studentID, courseID, courseNum):
db = connect()
cursor = db.cursor()
sql = f'DELETE FROM tb_student_course WHERE studentID = \'{studentID}\' ' \
f'and courseID = \'{courseID}\' and courseNum = \'{courseNum}\''
cursor.execute(sql)
try:
db.commit()
db.close()
return 0
except:
db.rollback()
db.close()
return 1
# 课程信息清除
def clear_course_info(self):
for x in self.resultTable.get_children():
self.resultTable.delete(x)
# 课程成绩区域
class stuGrade:
def __init__(self, frame, studentNum):
self.studentNum = studentNum
self.frame = frame
# 信息放置处, 滚动条
self.resultScrollbar = tkinter.Scrollbar(self.frame)
self.resultTable = ttk.Treeview(self.frame,
height=10,
columns=['课程号', '课程名', '课序号', '教师', '学分', '成绩'],
show='headings',
yscrollcommand=self.resultScrollbar.set)
self.initialize()
def initialize(self):
self.frame.place(relx=RADIO, relheight=1, relwidth=1-RADIO)
# 放置组件
self.frame.update()
self.frame.update()
self.resultTable.place(height=self.frame.winfo_height(),
relwidth=0.8)
self.displayInfo()
# 显示选课信息
def displayInfo(self):
self.clear_course_info()
self.resultTable.heading('课程号', text='课程号', anchor='w')
self.resultTable.heading('课程名', text='课程名')
self.resultTable.heading('课序号', text='课序号')
self.resultTable.heading('教师', text='教师')
self.resultTable.heading('学分', text='学分')
self.resultTable.heading('成绩', text='成绩')
self.resultTable.column('课程号', width=50, minwidth=50)
self.resultTable.column('课程名', width=120, minwidth=120)
self.resultTable.column('课序号', width=50, minwidth=50)
self.resultTable.column('教师', width=50, minwidth=50)
self.resultTable.column('学分', width=50, minwidth=50)
self.resultTable.column('成绩', width=50, minwidth=50)
info = self.get_class_selected()
for x in info:
self.resultTable.insert('', tkinter.END, values=x)
# 获取已选课程
def get_class_selected(self):
db = connect()
cursor = db.cursor()
sql = f'select SC.courseID, C.courseName, SC.courseNum, T.teacherName, C.courseCredit, SC.grade ' \
f'from tb_student_course as SC, tb_teacher as T, tb_teacher_course as TC, tb_course as C ' \
f'where SC.studentID = \'{self.studentNum}\' and SC.courseNum = C.courseNum and SC.courseID = C.courseID ' \
'and T.teacherID = TC.teacherID and TC.courseID = C.courseID and TC.courseNum = C.courseNum'
cursor.execute(sql)
result = cursor.fetchall()
db.close()
return result
def clear_course_info(self):
for x in self.resultTable.get_children():
self.resultTable.delete(x)
if __name__ == '__main__':
c = menu('202101')
c.start()