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 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
| import tkinter as tk from tkinter import messagebox, PhotoImage, Label, Canvas, Frame import pymysql
class MainApplication(tk.Tk): def __init__(self): super().__init__() self.geometry("700x650+99+50") self.title("学生管理系统 v1.1.0")
self.connection = self.connect_to_database()
bg_image = PhotoImage(file="bg_image.png") canvas = Canvas(self, width=700, height=650) canvas.pack(fill="both", expand=True) canvas.create_image(0, 0, image=bg_image, anchor="nw") canvas.image = bg_image
frame = tk.Frame(canvas, bg='lightblue', bd=2, relief=tk.SUNKEN) canvas.create_window(350, 325, window=frame)
word_list = ["维护学生信息", "维护课程信息", "维护学生成绩", "统计学生成绩", "学生成绩排名", "查询学生信息"] for i, word in enumerate(word_list): button = tk.Button( frame, text=word, bg='SlateGray', fg='AliceBlue', bd=5, command=lambda i=i: self.show_sub_window(i), width=20, font=('宋体', 12), activeforeground="pink", activebackground="LavenderBlush" ) button.grid(row=i, column=0, pady=10, padx=20)
def connect_to_database(self): """创建数据库连接""" try: connection = pymysql.connect( host='localhost', user='root', password='root', database='s_t_u202312315', charset='utf8mb4', autocommit=True ) return connection except pymysql.MySQLError as e: messagebox.showerror("连接失败", f"数据库连接错误: {e}") self.destroy()
def show_sub_window(self, button_id): """打开子窗口""" if button_id == 0: SubWindow1(self, self.connection) elif button_id == 1: SubWindow2(self, self.connection) elif button_id == 2: SubWindow3(self, self.connection) elif button_id == 3: SubWindow4(self, self.connection) elif button_id == 4: SubWindow5(self, self.connection) elif button_id == 5: SubWindow6(self, self.connection)
class SubWindow1(tk.Toplevel): def __init__(self, master, connection): super().__init__(master) self.title("维护学生信息") self.geometry("450x635+900+50") self.connection = connection
bg_image = PhotoImage(file="bg_image.png") background_label = Label(self, image=bg_image) background_label.place(relwidth=1, relheight=1) background_label.image = bg_image
input_frame = Frame(self, bg="#ffffff") input_frame.place(relx=0.5, rely=0.4, anchor="center")
fields = ["学号:", "姓名:", "性别:", "年龄:", "系:", "奖学金:"] self.entries = [] for i, field in enumerate(fields): label = tk.Label(input_frame, text=field, font=('Times New Roman', '12'), bg='white', fg='Black') label.grid(row=i, column=0, padx=10, pady=15, sticky="e") entry = tk.Entry(input_frame, font=('Times New Roman', '12'), bd=3, bg='lightyellow', fg='Black', width=25) entry.grid(row=i, column=1, padx=10, pady=15, sticky="w") self.entries.append(entry)
button_frame = Frame(self, bg="white") button_frame.place(relx=0.5, rely=0.85, anchor="center")
insert_button = tk.Button( button_frame, text="确认添加", command=self.insert_student_table, font=('宋体', '14'), bg='lightblue', fg='White', bd=6, activeforeground="SpringGreen", activebackground="Wheat" ) insert_button.grid(row=0, column=0, padx=20)
update_button = tk.Button( button_frame, text="确认修改", command=self.update_student_table, font=('宋体', '14'), bg='lightblue', fg='White', bd=6, activeforeground="SpringGreen", activebackground="Wheat" ) update_button.grid(row=0, column=1, padx=20)
def insert_student_table(self): """新增学生信息的逻辑""" new_sno = self.entries[0].get() new_sname = self.entries[1].get() new_ssex = self.entries[2].get() new_sage = self.entries[3].get() new_sdept = self.entries[4].get() new_scholarship = self.entries[5].get() try: with self.connection.cursor() as cursor: sql = ("INSERT INTO student(sno, sname, ssex, sage, sdept, scholarship) " "values (%s, %s, %s, %s, %s, %s);") cursor.execute(sql, (new_sno, new_sname, new_ssex, int(new_sage), new_sdept, new_scholarship)) messagebox.showinfo("成功", f"已加入新生{new_sno}信息!") except pymysql.MySQLError as e: messagebox.showerror("错误", f"新增失败: {e}")
def update_student_table(self): """更新学生信息的逻辑""" new_sno = self.entries[0].get() new_sname = self.entries[1].get() new_ssex = self.entries[2].get() new_sage = self.entries[3].get() new_sdept = self.entries[4].get() new_scholarship = self.entries[5].get() try: with self.connection.cursor() as cursor: sql = ("UPDATE student SET sname=%s, ssex=%s, sage=%s, sdept=%s, scholarship=%s WHERE sno=%s;") cursor.execute(sql, (new_sname, new_ssex, int(new_sage), new_sdept, new_scholarship, new_sno)) messagebox.showinfo("成功", f"已更新学生{new_sno}信息!") except pymysql.MySQLError as e: messagebox.showerror("错误", f"更新失败: {e}")
class SubWindow2(tk.Toplevel): def __init__(self, master, connection): super().__init__(master) self.title("维护课程信息") self.geometry("450x635+900+50") self.connection = connection
bg_image = PhotoImage(file="bg_image.png") background_label = Label(self, image=bg_image) background_label.place(relwidth=1, relheight=1) background_label.image = bg_image
input_frame = Frame(self, bg="white") input_frame.place(relx=0.5, rely=0.4, anchor="center")
fields = ["课程号:", "课程名:", "先行课:", "学分:"] self.entries = [] for i, field in enumerate(fields): label = tk.Label(input_frame, text=field, font=('Times New Roman', '12'), bg='white', fg='Black') label.grid(row=i, column=0, padx=10, pady=15, sticky="e") entry = tk.Entry(input_frame, font=('Times New Roman', '12'), bd=3, bg='lightyellow', fg='Black', width=25) entry.grid(row=i, column=1, padx=10, pady=15, sticky="w") self.entries.append(entry)
button_frame = Frame(self, bg="white") button_frame.place(relx=0.5, rely=0.85, anchor="center")
insert_button = tk.Button( button_frame, text="确认添加", command=self.insert_course, font=('宋体', '14'), bg='lightblue', fg='White', bd=6, activeforeground="SpringGreen", activebackground="Wheat" ) insert_button.grid(row=0, column=0, padx=20)
update_button = tk.Button( button_frame, text="确认修改", command=self.update_course, font=('宋体', '14'), bg='lightblue', fg='White', bd=6, activeforeground="SpringGreen", activebackground="Wheat" ) update_button.grid(row=0, column=1, padx=20)
delete_button = tk.Button( button_frame, text="确认删除", command=self.update_course, font=('宋体', '14'), bg='lightblue', fg='White', bd=6, activeforeground="SpringGreen", activebackground="Wheat" ) delete_button.grid(row=0, column=2, padx=20)
tk.Button(button_frame, text="确认添加", command=self.insert_course, font=('宋体', '14')).grid(row=0, column=0, padx=10) tk.Button(button_frame, text="确认修改", command=self.update_course, font=('宋体', '14')).grid(row=0, column=1, padx=10) tk.Button(button_frame, text="确认删除", command=self.delete_course, font=('宋体', '14')).grid(row=0, column=2, padx=10)
def insert_course(self): """新增课程信息""" new_cno = self.entries[0].get() new_cname = self.entries[1].get() new_cpno = self.entries[2].get() new_credit = self.entries[3].get() try: with self.connection.cursor() as cursor: sql = "INSERT INTO course (cno, cname, cpno, ccredit) VALUES (%s, %s, %s, %s)" cursor.execute(sql, (new_cno, new_cname, new_cpno, int(new_credit))) messagebox.showinfo("成功", f"已成功添加课程 {new_cno} 信息!") except pymysql.MySQLError as e: messagebox.showerror("错误", f"添加失败: {e}")
def update_course(self): """更新课程信息""" new_cno = self.entries[0].get() new_cname = self.entries[1].get() new_cpno = self.entries[2].get() new_credit = self.entries[3].get() try: with self.connection.cursor() as cursor: sql = "UPDATE course SET cname=%s, cpno=%s, ccredit=%s WHERE cno=%s" cursor.execute(sql, (new_cname, new_cpno, int(new_credit), new_cno)) messagebox.showinfo("成功", f"已更新课程 {new_cno} 信息!") except pymysql.MySQLError as e: messagebox.showerror("错误", f"更新失败: {e}")
def delete_course(self): """删除课程信息(无选课记录的课程)""" try: with self.connection.cursor() as cursor: sql = "DELETE FROM course WHERE NOT EXISTS (SELECT * FROM sc WHERE sc.cno=course.cno)" cursor.execute(sql) messagebox.showinfo("成功", "已删除无选课记录的课程信息!") except pymysql.MySQLError as e: messagebox.showerror("错误", f"删除失败: {e}")
class SubWindow3(tk.Toplevel): def __init__(self, master, connection): super().__init__(master) self.title("维护学生成绩") self.geometry("450x635+900+50") self.connection = connection
bg_image = PhotoImage(file="bg_image.png") background_label = Label(self, image=bg_image) background_label.place(relwidth=1, relheight=1) background_label.image = bg_image
input_frame = Frame(self, bg="white") input_frame.place(relx=0.5, rely=0.4, anchor="center")
fields = ["学号:", "课程号:", "成绩:"] self.entries = [] for i, field in enumerate(fields): label = tk.Label(input_frame, text=field, font=('Times New Roman', '12'), bg='white', fg='Black') label.grid(row=i, column=0, padx=10, pady=15, sticky="e") entry = tk.Entry(input_frame, font=('Times New Roman', '12'), bd=3, bg='lightyellow', fg='Black', width=25) entry.grid(row=i, column=1, padx=10, pady=15, sticky="w") self.entries.append(entry)
button_frame = Frame(self, bg="white") button_frame.place(relx=0.5, rely=0.85, anchor="center")
insert_button = tk.Button( button_frame, text="确认添加", command=self.insert_sc_table, font=('宋体', '14'), bg='lightblue', fg='White', bd=6, activeforeground="SpringGreen", activebackground="Wheat" ) insert_button.grid(row=0, column=0, padx=20)
update_button = tk.Button( button_frame, text="确认修改", command=self.update_sc_table, font=('宋体', '14'), bg='lightblue', fg='White', bd=6, activeforeground="SpringGreen", activebackground="Wheat" ) update_button.grid(row=0, column=1, padx=20)
tk.Button(button_frame, text="确认添加", command=self.insert_sc_table, font=('宋体', '14')).grid(row=0, column=0, padx=10) tk.Button(button_frame, text="确认修改", command=self.update_sc_table, font=('宋体', '14')).grid(row=0, column=1, padx=10)
def insert_sc_table(self): new_sno = self.entries[0].get() new_cno = self.entries[1].get() new_grade = self.entries[2].get() try: with self.connection.cursor() as cursor: sql = "INSERT INTO sc(sno, cno, grade) VALUES (%s, %s, %s);" cursor.execute(sql, (new_sno, new_cno, new_grade)) messagebox.showinfo("成功", f"已添加成绩信息!") except pymysql.MySQLError as e: messagebox.showerror("错误", f"添加失败: {e}")
def update_sc_table(self): new_sno = self.entries[0].get() new_cno = self.entries[1].get() new_grade = self.entries[2].get() try: with self.connection.cursor() as cursor: sql = "UPDATE sc SET grade=%s WHERE sno=%s AND cno=%s;" cursor.execute(sql, (new_grade, new_sno, new_cno)) self.connection.commit() messagebox.showinfo("成功", f"已更新成绩信息!") except pymysql.MySQLError as e: messagebox.showerror("错误", f"更新失败: {e}") except Exception as e: messagebox.showerror("错误", f"发生未知错误: {e}")
class SubWindow4(tk.Toplevel): def __init__(self, master, connection): super().__init__(master) self.title("统计学生成绩") self.geometry("450x635+900+50") self.connection = connection
bg_image = PhotoImage(file="bg_image.png") background_label = Label(self, image=bg_image) background_label.place(relwidth=1, relheight=1) background_label.image = bg_image
input_frame = Frame(self, bg="white") input_frame.place(relx=0.5, rely=0.4, anchor="center")
fields = ["系:", "课程号:"] self.entries = [] for i, field in enumerate(fields): label = tk.Label(input_frame, text=field, font=('Times New Roman', '12'), bg='white', fg='Black') label.grid(row=i, column=0, padx=10, pady=15, sticky="e") entry = tk.Entry(input_frame, font=('Times New Roman', '12'), bd=3, bg='lightyellow', fg='Black', width=25) entry.grid(row=i, column=1, padx=10, pady=15, sticky="w") self.entries.append(entry)
button_frame = Frame(self, bg="white") button_frame.place(relx=0.5, rely=0.85, anchor="center")
buttons = [ ("查询平均成绩", self.query_average_score), ("查询最高成绩", self.query_highest_score), ("查询最低成绩", self.query_lowest_score), ("查询不及格人数", self.query_fail_count), ("查询优秀率", self.query_excellent_rate) ]
for i, (text, command) in enumerate(buttons): button = tk.Button( button_frame, text=text, command=command, font=('宋体', '12'), bg='lightblue', fg='White', bd=6, activeforeground="SpringGreen", activebackground="Wheat" ) button.grid(row=i // 2, column=i % 2, padx=10, pady=10)
self.result_text = tk.Text(self, height=6, width=37, font=('Times New Roman', '12'), bg="lightyellow") self.result_text.place(relx=0.5, rely=0.6, anchor="center")
def query_average_score(self): """查询平均成绩""" sdept = self.entries[0].get() cno = self.entries[1].get() try: with self.connection.cursor() as cursor: sql = """ SELECT ROUND(AVG(grade), 2) FROM sc JOIN student ON sc.sno = student.sno WHERE student.sdept = %s AND sc.cno = %s; """ cursor.execute(sql, (sdept, cno)) result = cursor.fetchone() self.result_text.insert(tk.END, f"平均成绩: {result[0] if result[0] else '无数据'}\n") except pymysql.MySQLError as e: messagebox.showerror("错误", f"查询失败: {e}")
def query_highest_score(self): """查询最高成绩""" sdept = self.entries[0].get() cno = self.entries[1].get() try: with self.connection.cursor() as cursor: sql = """ SELECT MAX(grade) FROM sc JOIN student ON sc.sno = student.sno WHERE student.sdept = %s AND sc.cno = %s; """ cursor.execute(sql, (sdept, cno)) result = cursor.fetchone() self.result_text.insert(tk.END, f"最高成绩: {result[0] if result[0] else '无数据'}\n") except pymysql.MySQLError as e: messagebox.showerror("错误", f"查询失败: {e}")
def query_lowest_score(self): """查询最低成绩""" sdept = self.entries[0].get() cno = self.entries[1].get() try: with self.connection.cursor() as cursor: sql = """ SELECT MIN(grade) FROM sc JOIN student ON sc.sno = student.sno WHERE student.sdept = %s AND sc.cno = %s; """ cursor.execute(sql, (sdept, cno)) result = cursor.fetchone() self.result_text.insert(tk.END, f"最低成绩: {result[0] if result[0] else '无数据'}\n") except pymysql.MySQLError as e: messagebox.showerror("错误", f"查询失败: {e}")
def query_fail_count(self): """查询不及格人数""" sdept = self.entries[0].get() cno = self.entries[1].get() try: with self.connection.cursor() as cursor: sql = """ SELECT COUNT(*) FROM sc JOIN student ON sc.sno = student.sno WHERE student.sdept = %s AND sc.cno = %s AND grade < 60; """ cursor.execute(sql, (sdept, cno)) result = cursor.fetchone() self.result_text.insert(tk.END, f"不及格人数: {result[0] if result[0] else '0'}\n") except pymysql.MySQLError as e: messagebox.showerror("错误", f"查询失败: {e}")
def query_excellent_rate(self): """查询优秀率 (成绩 > 80)""" sdept = self.entries[0].get() cno = self.entries[1].get() try: with self.connection.cursor() as cursor: sql = """ SELECT COUNT(*) AS excellent_count, (SELECT COUNT(*) FROM sc WHERE cno = %s AND student.sdept = %s) AS total_count FROM sc JOIN student ON sc.sno = student.sno WHERE student.sdept = %s AND sc.cno = %s AND grade > 80; """ cursor.execute(sql, (cno, sdept, sdept, cno)) result = cursor.fetchone() excellent_count = result[0] if result[0] else 0 total_count = result[1] if result[1] else 0 excellent_rate = (excellent_count / total_count * 100) if total_count > 0 else 0 self.result_text.insert(tk.END, f"优秀率: {excellent_rate:.2f}%\n") except pymysql.MySQLError as e: messagebox.showerror("错误", f"查询失败: {e}")
class SubWindow5(tk.Toplevel): def __init__(self, master, connection): super().__init__(master) self.title("学生成绩排名") self.geometry("450x635+900+50") self.connection = connection
bg_image = PhotoImage(file="bg_image.png") background_label = Label(self, image=bg_image) background_label.place(relwidth=1, relheight=1) background_label.image = bg_image
input_frame = Frame(self, bg="white") input_frame.place(relx=0.5, rely=0.4, anchor="center")
fields = ["系:", "课程号:"] self.entries = [] for i, field in enumerate(fields): label = tk.Label(input_frame, text=field, font=('Times New Roman', '12'), bg='white', fg='Black') label.grid(row=i, column=0, padx=10, pady=15, sticky="e") entry = tk.Entry(input_frame, font=('Times New Roman', '12'), bd=3, bg='lightyellow', fg='Black', width=25) entry.grid(row=i, column=1, padx=10, pady=15, sticky="w") self.entries.append(entry)
button_frame = Frame(self, bg="white") button_frame.place(relx=0.5, rely=0.85, anchor="center")
rank_button = tk.Button( button_frame, text="查询成绩排名", command=self.query_rankings, font=('宋体', '14'), bg='lightblue', fg='White', bd=6, activeforeground="SpringGreen", activebackground="Wheat" ) rank_button.grid(row=0, column=0, padx=20)
self.result_text = tk.Text(self, height=6, width=37, font=('Times New Roman', '12'), bg="lightyellow") self.result_text.place(relx=0.5, rely=0.6, anchor="center")
def query_rankings(self): """查询学生成绩排名""" sdept = self.entries[0].get() cno = self.entries[1].get() try: with self.connection.cursor() as cursor: sql = """ SELECT student.sno, student.sname, sc.grade FROM sc JOIN student ON sc.sno = student.sno WHERE student.sdept = %s AND sc.cno = %s ORDER BY sc.grade DESC; """ cursor.execute(sql, (sdept, cno)) results = cursor.fetchall() self.result_text.delete(1.0, tk.END) if results: for rank, (sno, sname, grade) in enumerate(results, start=1): self.result_text.insert(tk.END, f"第{rank}名: {sno} {sname} 成绩: {grade}\n") else: self.result_text.insert(tk.END, "无数据") except pymysql.MySQLError as e: messagebox.showerror("错误", f"查询失败: {e}")
class SubWindow6(tk.Toplevel): def __init__(self, master, connection): super().__init__(master) self.title("查询学生信息") self.geometry("450x635+900+50") self.connection = connection
bg_image = PhotoImage(file="bg_image.png") background_label = Label(self, image=bg_image) background_label.place(relwidth=1, relheight=1) background_label.image = bg_image
input_frame = Frame(self, bg="white") input_frame.place(relx=0.5, rely=0.4, anchor="center")
fields = ["学号:", "姓名:"] self.entries = [] for i, field in enumerate(fields): label = tk.Label(input_frame, text=field, font=('Times New Roman', '12'), bg='white', fg='Black') label.grid(row=i, column=0, padx=10, pady=15, sticky="e") entry = tk.Entry(input_frame, font=('Times New Roman', '12'), bd=3, bg='lightyellow', fg='Black', width=25) entry.grid(row=i, column=1, padx=10, pady=15, sticky="w") self.entries.append(entry)
button_frame = Frame(self, bg="white") button_frame.place(relx=0.5, rely=0.85, anchor="center")
search_button = tk.Button( button_frame, text="查询学生信息", command=self.query_student_info, font=('宋体', '14'), bg='lightblue', fg='White', bd=6, activeforeground="SpringGreen", activebackground="Wheat" ) search_button.grid(row=0, column=0, padx=20)
self.result_text = tk.Text(self, height=7, width=37, font=('Times New Roman', '12'), bg="lightyellow") self.result_text.place(relx=0.5, rely=0.6, anchor="center")
def query_student_info(self): """查询学生信息""" sno = self.entries[0].get().strip() sname = self.entries[1].get().strip()
try: with self.connection.cursor() as cursor: if sno: sql = "SELECT * FROM Student WHERE sno = %s" cursor.execute(sql, (sno,)) elif sname: sql = "SELECT * FROM Student WHERE sname = %s" cursor.execute(sql, (sname,)) else: self.result_text.insert(tk.END, "请输入学号或姓名进行查询。\n") return
student_info = cursor.fetchone() self.result_text.delete(1.0, tk.END) if student_info: sno, sname, ssex, sage, sdept, scholarship = student_info self.result_text.insert( tk.END, f"学号: {sno}\n姓名: {sname}\n性别: {ssex}\n年龄: {sage}\n系别: {sdept}\n奖学金:{scholarship}\n" ) course_sql = """ SELECT c.cname FROM SC sc JOIN Course c ON sc.cno = c.cno WHERE sc.sno = %s """ cursor.execute(course_sql, (sno,)) courses = cursor.fetchall() if courses: self.result_text.insert(tk.END, "选课信息:") for course in courses: self.result_text.insert(tk.END, f"- {course[0]}\n") else: self.result_text.insert(tk.END, "无选课信息。\n") else: self.result_text.insert(tk.END, "无匹配的学生信息。\n") except pymysql.MySQLError as e: messagebox.showerror("错误", f"查询失败: {e}")
if __name__ == "__main__": app = MainApplication() app.mainloop()
|