import tkinter from tkinter import * from tkinter import messagebox from tkinter.messagebox import * from tkinter.ttk import Treeview import random import pymysql import time class StuSc: def __init__(self, master, no): self.window1 = None self.course_array = [] self.exam_array = [] self.id = no self.flag = 0 self.que_no = 1 self.root = master self.root.geometry('1100x500') self.root.title('考生主页') self.window = Frame(self.root) self.window.pack() self.main_menu = Menu(self.window) self.main_menu.add_cascade(label="考试", command=self.show_course) self.main_menu.add_command(label="查询成绩", command=self.sel_score) self.main_menu.add_command(label="修改密码", command=self.chan_pw) self.main_menu.add_command(label="发送留言", command=self.SendM) self.UserOldInput = Entry(self.window) self.UserNewInput = Entry(self.window, show='*') self.root.config(menu=self.main_menu) def show_course(self): if self.flag == 1: messagebox.showerror(title='警告', message='请专注于当前考试') else: self.window.pack_forget() self.window = Frame(self.root) self.window.pack() # 点击 考试列表 时单击响应 def on_click_exam_list_tree(event): if self.flag == 1: messagebox.showerror(title='警告', message='请专注于当前考试') else: selected_item = event.widget.selection() exam_id = self.exam_array[int(selected_item[0]) - 1][0] ask = askyesno(title="考试提醒", message="是否确定进入?", ) if ask: self.flag = 1 timestamp = int(time.time()) time_local = time.localtime(timestamp) timetable = time.strftime("%Y-%m-%d %H:%M:%S", time_local) self.Exam(exam_id, timetable) # 点击 课程列表 时的单击响应函数 def on_click_course_list_tree(event): if self.flag == 1: messagebox.showerror(title='警告', message='请专注于当前考试') else: selected_item = event.widget.selection() # event.widget获取Treeview对象,调用selection获取选择对象名称 # 清空exam_select_tree上现有的内容 # 清空exam_select_tree上现有的内容 exam_select_tree.delete(*exam_select_tree.get_children()) course_name = self.course_array[int(selected_item[0])-1][1] ad_id = self.course_array[int(selected_item[0])-1][2] conn1 = pymysql.connect(host='localhost', user='root', password='123456', db='ruangong', charset='utf8') cur1 = conn1.cursor() search_sqi1 = "select ExamID,ExamName from examnation,Course where CourseName=\""+course_name + \ "\" and examnation.AdID=\"" + ad_id + "\"and examnation.CourseID=Course.CourseID;" cur1.execute(search_sqi1) j = 1 for row1 in cur1: exam_select_tree.insert("", 'end', str(j), text='', values=row1) j = j+1 self.exam_array.append(row1) exam_select_tree.update_idletasks() cur1.close() conn1.close() main_panedwindow = PanedWindow(self.window, sashrelief=RAISED, sashwidth=5, orient=VERTICAL) # 课程列表区 course_list_frame = Frame() course_list_sub_frame = Frame(course_list_frame) course_list_tree = Treeview(course_list_sub_frame, selectmode='browse') course_list_tree.bind('<>', on_click_course_list_tree) # 课程列表垂直滚动条 course_list_vsc = Scrollbar(course_list_sub_frame, orient="vertical", command=course_list_tree.yview) course_list_vsc.pack(side=RIGHT, fill=Y, expand=YES) course_list_tree.configure(yscrollcommand=course_list_vsc.set) course_list_sub_frame.pack(side=TOP, fill=BOTH, expand=YES) # 课程列表水平滚动条 course_list_hsc = Scrollbar(course_list_frame, orient="horizontal", command=course_list_tree.xview) course_list_hsc.pack(side=BOTTOM, fill=X, expand=YES) course_list_tree.configure(xscrollcommand=course_list_hsc.set) # 课程列表区列标题 course_list_tree["columns"] = ("CourseID", "CourseName", "AdID") course_list_column_width = [333, 333, 333] course_list_tree['show'] = 'headings' # 载入列标题 for column_name, column_width in zip(course_list_tree["columns"], course_list_column_width): course_list_tree.column(column_name, width=column_width, anchor='w') course_list_tree.heading(column_name, text=column_name) course_list_tree.pack(side=LEFT, fill=X, expand=YES) course_list_frame.pack(side=TOP, fill=X, padx=5, pady=5, expand=YES, anchor='n') # 将数据包列表区加入到主窗体 main_panedwindow.add(course_list_frame) conn = pymysql.connect(host='localhost', user='root', password='123456', db='ruangong', charset='utf8') cur = conn.cursor() search_sqi = "select * from Course;" cur.execute(search_sqi ) i = 1 for row in cur: course_list_tree.insert("", "end", str(i), values=row) i = i + 1 self.course_array.append(row) course_list_tree.update_idletasks() cur.close() conn.close() # 考试选择区 exam_select_frame = Frame() exam_select_sub_frame = Frame(exam_select_frame) exam_select_tree = Treeview(exam_select_sub_frame, selectmode='browse') exam_select_tree.bind('<>', on_click_exam_list_tree) exam_select_tree.pack(side=LEFT, fill=BOTH, expand=YES) # 考试选择区垂直滚动条 exam_select_vsc = Scrollbar(exam_select_sub_frame, orient="vertical", command=exam_select_tree.yview) exam_select_vsc.pack(side=RIGHT, fill=Y) exam_select_tree.configure(yscrollcommand=exam_select_vsc.set) exam_select_sub_frame.pack(side=TOP, fill=X, expand=YES) # 考试选择区水平滚动条 exam_select_hsc = Scrollbar(exam_select_frame, orient="horizontal", command=exam_select_tree.xview) exam_select_hsc.pack(side=BOTTOM, fill=X) exam_select_tree.configure(xscrollcommand=exam_select_hsc.set) exam_select_frame.pack(side=LEFT, fill=X, padx=5, pady=5, expand=YES) # 载入列标题 exam_select_tree["columns"] = ("ExamID", "ExamName") exam_select_column_width = [500, 500] exam_select_tree['show'] = 'headings' for column_name, column_width in zip(exam_select_tree["columns"], exam_select_column_width): exam_select_tree.column(column_name, width=column_width, anchor='w') exam_select_tree.heading(column_name, text=column_name) exam_select_tree.pack(side=LEFT, fill=X, expand=YES) exam_select_frame.pack(side=TOP, fill=X, padx=5, pady=5, expand=YES, anchor='n') # 将考试列表区加入到主窗体 main_panedwindow.add(exam_select_frame) main_panedwindow.pack(fill=BOTH, expand=1) def sel_score(self): if self.flag == 1: messagebox.showerror(title='警告', message='请专注于当前考试') else: self.window.pack_forget() self.window = Frame(self.root) self.window.pack() main_panedwindow = PanedWindow(self.window, sashrelief=RAISED, sashwidth=5, orient=VERTICAL) # 课程列表区 score_list_frame = Frame() score_list_sub_frame = Frame(score_list_frame) score_list_tree = Treeview(score_list_sub_frame, selectmode='browse') # # 课程列表垂直滚动条 # score_list_vsc = Scrollbar(score_list_sub_frame, orient="vertical", command=score_list_tree.yview) # score_list_vsc.pack(side=RIGHT, fill=Y, expand=YES) # score_list_tree.configure(yscrollcommand=score_list_vsc.set) # score_list_sub_frame.pack(side=TOP, fill=BOTH, expand=YES) # # # 课程列表水平滚动条 # score_list_hsc = Scrollbar(score_list_frame, orient="horizontal", command=score_list_tree.xview) # score_list_hsc.pack(side=BOTTOM, fill=X, expand=YES) # score_list_tree.configure(xscrollcommand=score_list_hsc.set) # 课程列表垂直滚动条 score_list_vsc = Scrollbar(score_list_sub_frame, orient="vertical", command=score_list_tree.yview) score_list_vsc.pack(side=RIGHT, fill=Y, expand=YES) score_list_tree.configure(yscrollcommand=score_list_vsc.set) score_list_sub_frame.pack(side=TOP, fill=BOTH, expand=YES) # 课程列表水平滚动条 score_list_hsc = Scrollbar(score_list_frame, orient="horizontal", command=score_list_tree.xview) score_list_hsc.pack(side=BOTTOM, fill=X, expand=YES) score_list_tree.configure(xscrollcommand=score_list_hsc.set) # 课程列表区列标题 score_list_tree["columns"] = ('ExamName', 'Grade', 'beginTime', 'endTime') score_list_column_width = [250, 250, 250, 250] score_list_tree['show'] = 'headings' # 载入列标题 for column_name, column_width in zip(score_list_tree["columns"], score_list_column_width): score_list_tree.column(column_name, width=column_width, anchor='w') score_list_tree.heading(column_name, text=column_name) score_list_tree.pack(side=LEFT, fill=X, expand=YES) score_list_frame.pack(side=TOP, fill=X, padx=5, pady=5, expand=YES, anchor='n') # 将数据包列表区加入到主窗体 main_panedwindow.add(score_list_frame) main_panedwindow.pack(fill=BOTH, expand=1) conn = pymysql.connect(host='localhost', user='root', password='123456', db='ruangong', charset='utf8') cur = conn.cursor() search_sqi = "select ExamName,grade,beginTime,endTime from studentscore where " \ "studentId=\"" + self.id + "\";" cur.execute(search_sqi) i = 1 for row in cur: score_list_tree.insert("", "end", str(i), values=row) i = i + 1 score_list_tree.update_idletasks() cur.close() conn.close() def chan_pw(self): if self.flag == 1: messagebox.showerror(title='警告', message='请专注于当前考试') else: self.window.pack_forget() self.window = Frame(self.root) self.window.pack() Label(self.window, text='请输入旧密码: ', font=('宋体', 15)).grid(row=0, column=0, padx=70, pady=70) self.UserOldInput = Entry(self.window) self.UserOldInput.grid(row=0, column=1, padx=75, pady=75) Label(self.window, text='请输入新密码: ', font=('宋体', 15)).grid(row=1, column=0, padx=70, pady=70) self.UserNewInput = Entry(self.window, show='*') self.UserNewInput.grid(row=1, column=1, padx=75, pady=75) Button(self.window, text='确认', width=10, command=self.confirm, font=('宋体', 15)).grid(row=2, column=0, padx=70, pady=70) def confirm(self): a = self.UserOldInput.get() b = self.UserNewInput.get() conn = pymysql.connect(host='localhost', user='root', password='123456', db='ruangong', charset='utf8') cur = conn.cursor() search_sqi = "set sql_safe_updates=0;" cur.execute(search_sqi) search_sqi = "select studentPassword from student where studentId=\"" + self.id + "\";" cur.execute(search_sqi) info = cur.fetchone() if a == info[0] and b != info[0]: search_sqi = "update student set studentPassword =\"" + b + "\" where studentId=\"" + self.id + "\";" cur.execute(search_sqi) conn.commit() tkinter.messagebox.showinfo(title='提示', message='密码修改成功!') elif a != info[0]: tkinter.messagebox.showerror(title='警告', message='旧密码输入错误') else: tkinter.messagebox.showinfo(title='提示', message='新旧密码不能一样!') cur.close() conn.close() def Exam(self, exam_id, begin_time): def Ques_ABCD(num): self.que_no = num no = "第" + str(self.que_no) + "题选项" ts["text"] = no if user_answer[num - 1] == 'A': bta["bg"] = mychoicecolor btb["bg"] = choicecolor btc["bg"] = choicecolor btd["bg"] = choicecolor elif user_answer[num - 1] == 'B': bta["bg"] = choicecolor btb["bg"] = mychoicecolor btc["bg"] = choicecolor btd["bg"] = choicecolor elif user_answer[num - 1] == 'C': bta["bg"] = choicecolor btb["bg"] = choicecolor btc["bg"] = mychoicecolor btd["bg"] = choicecolor elif user_answer[num - 1] == 'D': bta["bg"] = choicecolor btb["bg"] = choicecolor btc["bg"] = choicecolor btd["bg"] = mychoicecolor else: bta["bg"] = choicecolor btb["bg"] = choicecolor btc["bg"] = choicecolor btd["bg"] = choicecolor def Choice_change(y): if y == 'A': bta["bg"] = mychoicecolor btb["bg"] = choicecolor btc["bg"] = choicecolor btd["bg"] = choicecolor elif y == 'B': bta["bg"] = choicecolor btb["bg"] = mychoicecolor btc["bg"] = choicecolor btd["bg"] = choicecolor elif y == 'C': bta["bg"] = choicecolor btb["bg"] = choicecolor btc["bg"] = mychoicecolor btd["bg"] = choicecolor else: bta["bg"] = choicecolor btb["bg"] = choicecolor btc["bg"] = choicecolor btd["bg"] = mychoicecolor user_answer[self.que_no - 1] = y if self.que_no == 1: button1["bg"] = mychoicecolor elif self.que_no == 2: button2["bg"] = mychoicecolor elif self.que_no == 3: button3["bg"] = mychoicecolor elif self.que_no == 4: button4["bg"] = mychoicecolor elif self.que_no == 5: button5["bg"] = mychoicecolor elif self.que_no == 6: button6["bg"] = mychoicecolor elif self.que_no == 7: button7["bg"] = mychoicecolor elif self.que_no == 8: button8["bg"] = mychoicecolor elif self.que_no == 9: button9["bg"] = mychoicecolor else: button10["bg"] = mychoicecolor def Paperover_Confirm(): for op in user_answer: if op == 'E': tkinter.messagebox.showerror(title='警告', message='温馨提示,您还有题未作答!') break else: fraction = 0 for op in range(10): if standard_answer[number_list[op]] == user_answer[op]: fraction += 10 conn1 = pymysql.connect(host='localhost', user='root', password='123456', db='ruangong', charset='utf8') cur1 = conn1.cursor() # timestamp = int(time.time()) # time_local = time.localtime(timestamp) # timetable = time.strftime("%Y-%m-%d %H:%M:%S", time_local) # search_sqi1 = "set sql_safe_updates=0;" # cur1.execute(search_sqi1) # search_sqi1 = "select ExamName from examnation where ExamID=\"" + exam_id + "\";" # cur1.execute(search_sqi1) # inf1 = cur1.fetchone() # search_sqi1 = "insert into studentscore values (\"" + \ # self.id + "\",\"" + inf1[0] + "\"," + str(fraction) + ",\"" + begin_time + "\"," + "\"" \ # + timetable + "\");" timestamp = int(time.time()) time_local = time.localtime(timestamp) timetable = time.strftime("%Y-%m-%d %H:%M:%S", time_local) search_sqi1 = "set sql_safe_updates=0;" cur1.execute(search_sqi1) search_sqi1 = "select ExamName from examnation where ExamID=\"" + exam_id + "\";" cur1.execute(search_sqi1) inf1 = cur1.fetchone() search_sqi1 = "insert into studentscore values (\"" + \ self.id + "\",\"" + inf1[0] + "\"," + str(fraction) + ",\"" + begin_time + "\"," + "\"" \ + timetable + "\");" cur1.execute(search_sqi1) conn1.commit() cur1.close() conn1.close() tkinter.messagebox.showinfo(title='考试结束', message='你的总分是'+str(fraction)+'!') self.window.pack_forget() self.window = Frame(self.root) self.window.pack() self.flag = 0 # 将成绩存入数据库并关闭当前答题界面回到学生页面 self.window.pack_forget() self.window = Frame(self.root) self.window.pack() count = 1 maxCount = 10 user_answer = [] for i in range(maxCount): user_answer.append('E') standard_answer = [] space_list = [] conn = pymysql.connect(host='localhost', user='root', password='123456', db='ruangong', charset='utf8') cur = conn.cursor() search_sqi = "select * from examnation where ExamID=\"" + exam_id + "\";" cur.execute(search_sqi) info = cur.fetchone() for i in range(4, 14): search_sqi = "select * from questions where QuesId=\"" + info[i] + "\";" cur.execute(search_sqi) info1 = cur.fetchone() standard_answer.append(info1[7]) tab = '\n\t' content = info1[2] + tab + info1[3] + tab + info1[4] + tab + info1[5] + tab + info1[6] space_list.append(content) cur.close() conn.close() # 此时 space_list 变量存储的才是我们真正想要的题库数据 (数据清洗) # 随机产生索引 使用set集合是为了不随机产生相同的索引数字 # 创建一个空集合,用于存储产生不重复的随机数 用作索引题库(space_list) number_list = [] while True: # 如果number_set的长度等于最大题数,说明产生了我们想要的随机数个数 那就跳出死循环 if len(number_list) == maxCount: break # 将产生的随机数添加到number_list x = random.randint(0, len(space_list) - 1) if x not in number_list: number_list.append(x) # 并将产生的新题目存入到paper_list paper_list = [] for i in number_list: paper_list.append(space_list[i]) s = '' for i in number_list: s += str(count) + "、" + space_list[i] s += '\n' count += 1 # 创建滚动条,放到窗口的右侧, 填充Y竖直方向 main_panedwindow = PanedWindow(self.window, sashrelief=RAISED, sashwidth=20, orient=VERTICAL) # 题目列表区 course_list_frame1 = Frame() text = Text(course_list_frame1, width=100, height=25, wrap='char') text.insert("insert", f'{s}') text['state'] = 'disabled' text.pack() # course_list_frame1.pack(side=LEFT, fill=X, expand=YES) course_list_frame1.pack(side=TOP, fill=X, padx=5, pady=5, expand=YES, anchor='n') # 将数据列表区加入到主窗体 main_panedwindow.add(course_list_frame1) # 定义按钮点击前后颜色 choicecolor = 'DimGray' mychoicecolor = 'DeepSkyBlue' self.window1 = Frame(self.root) # 当前所做所做题号,初始化默认为1 button1 = Button(self.window1, width=5, text="1", command=lambda: Ques_ABCD(1), bg=choicecolor) button2 = Button(self.window1, width=5, text="2", command=lambda: Ques_ABCD(2), bg=choicecolor) button3 = Button(self.window1, width=5, text="3", command=lambda: Ques_ABCD(3), bg=choicecolor) button4 = Button(self.window1, width=5, text="4", command=lambda: Ques_ABCD(4), bg=choicecolor) button5 = Button(self.window1, width=5, text="5", command=lambda: Ques_ABCD(5), bg=choicecolor) button6 = Button(self.window1, width=5, text="6", command=lambda: Ques_ABCD(6), bg=choicecolor) button7 = Button(self.window1, width=5, text="7", command=lambda: Ques_ABCD(7), bg=choicecolor) button8 = Button(self.window1, width=5, text="8", command=lambda: Ques_ABCD(8), bg=choicecolor) button9 = Button(self.window1, width=5, text="9", command=lambda: Ques_ABCD(9), bg=choicecolor) button10 = Button(self.window1, width=5, text="10", command=lambda: Ques_ABCD(10), bg=choicecolor) button1.pack(side=LEFT, padx=10) button2.pack(side=LEFT, after=button1, padx=10, pady=5) button3.pack(side=LEFT, after=button2, padx=10, pady=5) button4.pack(side=LEFT, after=button3, padx=10, pady=5) button5.pack(side=LEFT, after=button4, padx=10, pady=5) button6.pack(side=LEFT, after=button5, padx=10, pady=5) button7.pack(side=LEFT, after=button6, padx=10, pady=5) button8.pack(side=LEFT, after=button7, padx=10, pady=5) button9.pack(side=LEFT, after=button8, padx=10, pady=5) button10.pack(side=LEFT, after=button9, padx=10, pady=5) bta = Button(self.window1, width=5, text="A", command=lambda: Choice_change('A'), bg=choicecolor) btb = Button(self.window1, width=5, text="B", command=lambda: Choice_change('B'), bg=choicecolor) btc = Button(self.window1, width=5, text="C", command=lambda: Choice_change('C'), bg=choicecolor) btd = Button(self.window1, width=5, text="D", command=lambda: Choice_change('D'), bg=choicecolor) t = "第" + str(self.que_no) + "题选项" ts = Label(self.window1, width=10, text=t, bg=mychoicecolor) ts.pack(side=LEFT, padx=10) # bta.pack(side=LEFT, after=ts, padx=10) # btb.pack(side=LEFT, after=bta, padx=10, pady=5) # btc.pack(side=LEFT, after=btb, padx=10, pady=5) # btd.pack(side=LEFT, after=btc, padx=10, pady=5) bta.pack(side=LEFT, after=ts, padx=10) btb.pack(side=LEFT, after=bta, padx=10, pady=5) btc.pack(side=LEFT, after=btb, padx=10, pady=5) btd.pack(side=LEFT, after=btc, padx=10, pady=5) Paperover_Confirm = Button(self.window1, width=8, text="提交试卷", bg="red", command=Paperover_Confirm) Paperover_Confirm.pack(side=LEFT, after=btd, padx=10, pady=5) main_panedwindow.add(self.window1) main_panedwindow.pack(fill=BOTH, expand=1) def SendM(self): self.window.pack_forget() self.window = Frame(self.root) self.window.pack() Label(self.window, text='请输入留言: ', font=('宋体', 15)).grid(row=0, column=0, padx=70, pady=70) self.MInput = Entry(self.window) self.MInput.grid(row=0, column=1, padx=75, pady=75) Label(self.window, text='请输入收件人: ', font=('宋体', 15)).grid(row=1, column=0, padx=70, pady=70) self.ReInput = Entry(self.window, show='*') self.ReInput.grid(row=1, column=1, padx=75, pady=75) Button(self.window, text='确认', width=10, command=self.confirmM, font=('宋体', 15)).grid(row=2, column=0, padx=70, pady=70) def confirmM(self): if len(self.MInput.get())==0 or len(len(self.ReInput.get()))==0: messagebox.showerror(title='提示', message='不允许为空!') else: messagebox.showinfo(title='提示',message='已发送!') if __name__ == '__main__': root = Tk() StuSc(root, '021154001') root.mainloop()