Python+MySQL+Tkinter设计学生信息管理系统(速成)

前言

最近Python课堂上布置了综合实训,实验目标是设计一个学生信息管理系统,笔者对GUI界面早有些兴趣,在查找一番资料后总结出基于Python语言要实现学生信息管理系统的两大法宝----MySQL&Tkiner,下面就让我简单复盘一下程序设计的全流程

MySQL部分

MySQL下载以及配置

建议去网上找找教程,这里我贴了连接以后审核不通过QAQ

照着教程安装后还需要下载pymysql的包,在终端cmd输入pip install pymysql即可

在配置过程中我们时不时会出现一些小问题,这里我找到了一篇个人认为比较好的解决方案

MySQL基本学习

为了能够快速入门MySQL,我们可以直接上手敲代码来加快学习速度,可以去b站找找那个38min的教学视频

数据库的学习

在创建数据库的时候我用了Navicat来进行图形化的数据库创建,进一步加快学习效率,同时方便调试程序时查看表内数据,安装连接可以去网上找找

学习连接可以自行查找

数据库的创建

打开界面后我们要新建一个连接,名称任意,设置好自己的一些配置信息,第一次使用的话默认即可,然后点击下方的测试连接测试数据库的连接是否正常

7bca40ec345a4b6daddc45aefec3f72e.png

正常显示如下

7d89383f501d4fd484bbef82811a5bbe.png

 接下来就可以生成各项表,在这里我根据作业要求生成了两个表,一个是管理员表MANAGER,一个是学生表STUDENT

8602a276c43d4759bc80a641b8849d71.png

具体数据内容如下

管理员包含namecode两个信息即可 

179526e3f4fa4020b1e9fd160304db33.png

学生则包含ID、姓名以及若干成绩

9759b2bc63fa4a569a15b9942366075d.png这样一个基本的数据库我们就创建好了 

GUI学习

Tkinter是Python自带的GUI编程包,因此无需下载

Tkinter学习

入门的话看这一篇即可

Python-GUI界面设计(tkinter)_梦之海岛的博客-CSDN博客_python窗口界面设计

代码介绍

连接数据库

这里我们将先前在图形化界面创建好的数据对应上即可,这样就连接完成了

#!/usr/bin/python3

from tkinter import *
from tkinter import messagebox
from PIL import Image,ImageTk
import tkinter
import pymysql
from tkinter import ttk

# 打开数据库连接
def Sql_link():
    #生成数据库
    db = pymysql.connect(host='localhost',user='root',password='321',database='testdb')
    # 使用 cursor() 方法创建一个游标对象
    cursor = db.cursor()
    return db,cursor

管理员注册

·利用Tk()创建窗口对象并配置

·定义好关联变量,这样在后期将数据输入Entry文本框时才是能够传输给数据库的变量

·创建按钮对象,回调函数中对数据库进行操作

·合理放置标签,文本框,按钮对象

·回调函数中,先利用SELECT * FROM **** WHERE **** 判断是否存在相同ID,不同的话则利用   get()接收要写入的内容

·利用INSERT **** VALUES **** 给数据赋值

#管理员注册
def create_manager():
    #创建窗口
    create_manager_root = Tk()
    create_manager_root.title("管理员用户创建")
    create_manager_root.config(width=600)
    create_manager_root.config(height=600)
    #定义关联变量
    manager_name = StringVar(create_manager_root, value='')
    manager_code = StringVar(create_manager_root, value='')
    #提示性标签
    labelmanager_name = Label(create_manager_root, text="管理员ID注册", font=("微软雅黑 -20"))
    labelmanager_code = Label(create_manager_root, text="管理员密码设置", font=("微软雅黑 -20"))
    #设定标签位置
    labelmanager_name.place(x=200, y=100, height=40, width=200)
    labelmanager_code.place(x=200, y=200, height=40, width=200)
    #定义录入信息文本框,以关联变量形式存储
    entrymanager_name = Entry((create_manager_root), textvariable=manager_name)
    entrymanager_name.place(x=200, y=150, height=40, width=200)
    entrymanager_code = Entry((create_manager_root), textvariable=manager_code)
    entrymanager_code.place(x=200, y=250, height=40, width=200)
    #录入信息回调函数
    def Button_Ok():
        #设置标志位判断是否存在ID重复
        flag = 0
        #连接数据库
        db, cur = Sql_link()
        #将得到的StringVar对象传值回来
        data_manager_name = str(entrymanager_name.get())
        data_manager_code = str(entrymanager_code.get())
        #判断是否ID重复
        search = cur.execute("SELECT * FROM manager WHERE name =  " + data_manager_name + ';')
        if(search > 0):
            flag = 1
        else:
            flag = 0
        if(flag==0):
            try:
                #写入数据
                sql1 = "INSERT INTO manager(name,code)"
                sql1 += "VALUES('%s','%s')"%(data_manager_name,data_manager_code)
                cur.execute(sql1)
                db.commit()
                messagebox.showinfo(title="恭喜",message="注册成功!!!")
                create_manager_root.destroy()
            except:
                messagebox.showerror(message="注册失败!!!")
        else:
              messagebox.showerror("该用户名已注册!!!")
    #确认以及退出按钮
    Ok_Button = Button(create_manager_root,text="确认",font=("微软雅黑 -20"),command=Button_Ok)
    Exit_Button = Button(create_manager_root, text="退出", font=("微软雅黑 -20"), command=create_manager_root.destroy)
    Ok_Button.place(x=75, y=350, height=40, width=200)
    Exit_Button.place(x=325, y=350, height=40, width=200)

管理员登录

和上面区别不大,这里用到的MySQL语句还是WHERE *** AND *** 用于判断ID和密码是否能够对应的上

#管理员登录
def load_manager():
    # 创建窗口
    load_manager_root = Tk()
    load_manager_root.title("管理员用户创建")
    load_manager_root.config(width=600)
    load_manager_root.config(height=600)
    # 定义关联变量
    manager_name = StringVar(load_manager_root, value='')
    manager_code = StringVar(load_manager_root, value='')
    # 提示性标签
    labelmanager_name = Label(load_manager_root, text="管理员ID", font=("微软雅黑 -20"))
    labelmanager_code = Label(load_manager_root, text="管理员密码", font=("微软雅黑 -20"))
    # 设定标签位置
    labelmanager_name.place(x=200, y=100, height=40, width=200)
    labelmanager_code.place(x=200, y=200, height=40, width=200)
    # 定义录入信息文本框,以关联变量形式存储
    entrymanager_name = Entry((load_manager_root), textvariable=manager_name)
    entrymanager_name.place(x=200, y=150, height=40, width=200)
    entrymanager_code = Entry((load_manager_root), textvariable=manager_code)
    entrymanager_code.place(x=200, y=250, height=40, width=200)
    def Button_Ok():
        #连接数据库
        db,cur = Sql_link()
        # 将得到的StringVar对象传值回来
        data_manager_name = str(entrymanager_name.get())
        data_manager_code = str(entrymanager_code.get())
        sql = "SELECT * FROM manager WHERE name=%s AND code='%s'"%(data_manager_name,data_manager_code)
        search = cur.execute(sql)
        if(search>0):
            messagebox.showinfo(title="恭喜",message="登录成功!!!")
            show_student()
            load_manager_root.destroy()
        else:
            messagebox.showerror(message="该账号不存在!")
    Ok_Button = Button(load_manager_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
    Exit_Button = Button(load_manager_root, text="退出", font=("微软雅黑 -20"), command=load_manager_root.destroy)
    Ok_Button.place(x=75, y=350, height=40, width=200)
    Exit_Button.place(x=325, y=350, height=40, width=200)

管理员信息修改

这里信息修改分为两个小模块,一个是账号删除,一个是修改密码,前者直接用DELETE命令

后者用UPDATE命令

#管理员信息修改
def change_manager():
    # 创建窗口
    change_manager_root = Tk()
    change_manager_root.title("管理员信息修改")
    change_manager_root.config(width=600)
    change_manager_root.config(height=600)
    #删除信息
    def delete_manager():
        #创建窗口
        delete_manager_root = Tk()
        delete_manager_root.title("管理员信息删除")
        delete_manager_root.config(width=600)
        delete_manager_root.config(height=600)

        manager_name = StringVar(delete_manager_root, value='')
        # 提示性标签
        labelmanager_name = Label(delete_manager_root, text="要删除的管理员ID", font=("微软雅黑 -20"))
        entrymanager_name = Entry(delete_manager_root, textvariable=manager_name)

        labelmanager_name.place(x=200, y=100, height=40, width=200)
        entrymanager_name.place(x=200, y=200, height=40, width=200)

        def Button_Ok():
            db,cur = Sql_link()
            manager_name = eval(entrymanager_name.get())
            #查找对应的ID号
            sql = "SELECT * FROM manager WHERE name = %s"%(manager_name)
            search = cur.execute(sql)
            if(search>0):
                messagebox.showinfo(message="删除成功!")
                #删除目标行
                sql1 = "DELETE FROM manager WHERE name = %s"%(manager_name)
                cur.execute(sql1)
                db.commit()
            else:
                messagebox.showerror(message="该用户不存在!")
        Ok_Button = Button(delete_manager_root, text="确认", font=("微软雅黑 -20"),command=Button_Ok)
        Exit_Button = Button(delete_manager_root, text="退出", font=("微软雅黑 -20"), command=delete_manager_root.destroy)
        Ok_Button.place(x=75, y=350, height=40, width=200)
        Exit_Button.place(x=325, y=350, height=40, width=200)
    #修改密码
    def change_code():
        #创建窗口
        change_code_root = Tk()
        change_code_root.title("管理员信息删除")
        change_code_root.config(width=600)
        change_code_root.config(height=600)

        manager_name = StringVar(change_code_root, value='')
        old_manager_code = StringVar(change_code_root, value='')

        # 提示性标签
        labelmanager_name = Label(change_code_root, text="要修改的管理员ID", font=("微软雅黑 -20"))
        entrymanager_name = Entry(change_code_root, textvariable=manager_name)
        labelold_manager_code = Label(change_code_root, text="请输入原密码", font=("微软雅黑 -20"))
        entryold_manager_code = Entry(change_code_root, textvariable=old_manager_code)

        labelmanager_name.place(x=200, y=50, height=40, width=200)
        entrymanager_name.place(x=200, y=150, height=40, width=200)
        labelold_manager_code.place(x=200, y=250, height=40, width=200)
        entryold_manager_code.place(x=200, y=350, height=40, width=200)

        def Button_Ok():
            db,cur = Sql_link()
            manager_name = eval(entrymanager_name.get())
            #匹配对应的ID
            sql = "SELECT * FROM manager WHERE name = %s"%(manager_name)
            search = cur.execute(sql)
            if(search>0):
                #创建新窗口
                input_code_root = Tk()
                input_code_root.title("管理员密码修改")
                input_code_root.config(width=600)
                input_code_root.config(height=600)
                #设置关联变量
                new_manager_code = StringVar(input_code_root, value='')
                labelnew_manager_code = Label(input_code_root, text="新的密码", font=("微软雅黑 -20"))
                entrynew_manager_code = Entry(input_code_root, textvariable=new_manager_code)
                #设置提示性标签
                labelnew_manager_code.place(x=200, y=200, height=40, width=200)
                entrynew_manager_code.place(x=200, y=300, height=40, width=200)
                def Button_Ok():
                    try:
                        new_manager_code = entrynew_manager_code.get()
                        messagebox.showinfo(message="修改成功!")
                        #利用UPDATE来修改密码
                        sql1 = "UPDATE manager SET code = %s WHERE name = %s" % (new_manager_code,manager_name)
                        cur.execute(sql1)
                        db.commit()
                    except:
                        messagebox.showerror(message="修改失败!")
                Ok_Button = Button(input_code_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
                Exit_Button = Button(input_code_root, text="退出", font=("微软雅黑 -20"), command=input_code_root.destroy)
                Ok_Button.place(x=75, y=350, height=40, width=200)
                Exit_Button.place(x=325, y=350, height=40, width=200)

            else:
                messagebox.showerror(message="该用户不存在!")
        Ok_Button = Button(change_code_root, text="确认", font=("微软雅黑 -20"),command=Button_Ok)
        Exit_Button = Button(change_code_root, text="退出", font=("微软雅黑 -20"), command=change_code_root.destroy)
        Ok_Button.place(x=75, y=450, height=40, width=200)
        Exit_Button.place(x=325, y=450, height=40, width=200)
    # 设置按钮
    button_stu1 = Button(change_manager_root, text="删除账号", font=("微软雅黑 -20"),command=delete_manager)
    button_stu2 = Button(change_manager_root, text="修改密码", font=("微软雅黑 -20"),command=change_code)
    button_stu1.place(x=200, y=100, height=40, width=200)
    button_stu2.place(x=200, y=200, height=40, width=200)

    Exit_Button = Button(change_manager_root, text="退出", font=("微软雅黑 -20"), command=change_manager_root.destroy)
    Exit_Button.place(x=200, y=300, height=40, width=200)

展示学生信息页面

先初始化好展示学生信息的界面

def show_student():
    #创建窗口
    show_student_root = Tk()
    show_student_root.title("录入学生信息")
    show_student_root.config(width=600)
    show_student_root.config(height=600)

#设置按钮
    button_stu1 = Button(show_student_root, text="录入成绩信息", font=("微软雅黑 -20"),command=input_grade)
    button_stu2 = Button(show_student_root, text="查询成绩信息", font=("微软雅黑 -20"),command=search_grade)
    button_stu3 = Button(show_student_root, text="删除成绩信息", font=("微软雅黑 -20"),command=delete_data)
    button_stu4 = Button(show_student_root, text="分科成绩排序", font=("微软雅黑 -20"),command=sort_one_grade)
    button_stu5 = Button(show_student_root, text="总科成绩排序", font=("微软雅黑 -20"),command=sort_all_grade)
    button_stu1.place(x=200, y=30, height=40, width=200)
    button_stu2.place(x=200, y=130, height=40, width=200)
    button_stu3.place(x=200, y=230, height=40, width=200)
    button_stu4.place(x=200, y=330, height=40, width=200)
    button_stu5.place(x=200, y=430, height=40, width=200)

    Exit_Button = Button(show_student_root, text="退出", font=("微软雅黑 -20"), command=show_student_root.destroy)
    Exit_Button.place(x=200, y=530, height=40, width=200)

录入成绩信息

这里继续通过INSERT插入信息,同时利用加法将总分求出来

#输入信息
    def input_grade():
        input_grade_root = Tk()
        input_grade_root.title("学生成绩录入")
        input_grade_root.config(width=600)
        input_grade_root.config(height=900)
        # 定义关联变量
        student_id = StringVar(input_grade_root, value='')
        student_name = StringVar(input_grade_root, value='')
        math_grade = StringVar(input_grade_root, value='')
        python_grade = StringVar(input_grade_root, value='')
        modian_grade = StringVar(input_grade_root, value='')
        shudian_grade = StringVar(input_grade_root, value='')
        # 提示性标签
        labelstudent_id = Label(input_grade_root, text="学生ID", font=("微软雅黑 -20"))
        labelstudent_name = Label(input_grade_root, text="学生姓名", font=("微软雅黑 -20"))
        labelmath_grade = Label(input_grade_root, text="数学成绩", font=("微软雅黑 -20"))
        labelpython_grade = Label(input_grade_root, text="Python成绩", font=("微软雅黑 -20"))
        labelmodian_grade = Label(input_grade_root, text="模电成绩", font=("微软雅黑 -20"))
        labelshudian_grade = Label(input_grade_root, text="数电成绩", font=("微软雅黑 -20"))
        # 设定标签位置
        labelstudent_id.place(x=200, y=50, height=40, width=200)
        labelstudent_name.place(x=200, y=150, height=40, width=200)
        labelmath_grade.place(x=200, y=250, height=40, width=200)
        labelpython_grade.place(x=200, y=350, height=40, width=200)
        labelmodian_grade.place(x=200, y=450, height=40, width=200)
        labelshudian_grade.place(x=200, y=550, height=40, width=200)
        # 定义录入信息文本框,以关联变量形式存储
        entrystudent_id = Entry(input_grade_root, textvariable=student_id)
        entrystudent_id.place(x=200, y=100, height=40, width=200)
        entrystudent_name = Entry(input_grade_root, textvariable=student_name)
        entrystudent_name.place(x=200, y=200, height=40, width=200)
        entrymath_grade = Entry(input_grade_root, textvariable=math_grade)
        entrymath_grade.place(x=200, y=300, height=40, width=200)
        entrypython_grade = Entry(input_grade_root, textvariable=python_grade)
        entrypython_grade.place(x=200, y=400, height=40, width=200)
        entrymodian_grade = Entry(input_grade_root, textvariable=modian_grade)
        entrymodian_grade.place(x=200, y=500, height=40, width=200)
        entryshudian_grade = Entry(input_grade_root, textvariable=shudian_grade)
        entryshudian_grade.place(x=200, y=600, height=40, width=200)
        # 确认以及退出按钮
        def Button_Ok():
            #注意数据类型转换
            db,cur = Sql_link()
            student_id = eval(entrystudent_id.get())
            student_name = entrystudent_name.get()
            math_grade = eval(entrymath_grade.get())
            python_grade = eval(entrypython_grade.get())
            modian_grade = eval(entrymodian_grade.get())
            shudian_grade = eval(entryshudian_grade.get())
            try:
                #这里总分通过求和得到结果
                sql = "INSERT INTO student(ID,NAME,数学成绩,Python成绩,模电成绩,数电成绩,总分)"
                sql += "VALUES(%d,'%s',%.2f,%.2f,%.2f,%.2f,%.2f)"%(
                    student_id,student_name,math_grade,python_grade,modian_grade,
                    shudian_grade,math_grade+python_grade+modian_grade+shudian_grade)
                cur.execute(sql)
                db.commit()
                messagebox.showinfo(message="录入信息成功!")
            except:
                messagebox.showerror(message="录入信息失败!请检查是否有ID重复")
        Ok_Button = Button(input_grade_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
        Exit_Button = Button(input_grade_root, text="退出", font=("微软雅黑 -20"), command=input_grade_root.destroy)
        Ok_Button.place(x=75, y=700, height=40, width=200)
        Exit_Button.place(x=325, y=700, height=40, width=200)

学生信息查询

        def Button_Ok():
            show_student_message_root = Tk()
            show_student_message_root.title("学生信息查询")
            show_student_message_root.config(width=600)
            show_student_message_root.config(height=700)
            #创建关联变量
            student_name = StringVar(show_student_message_root, value='')
            math_grade = StringVar(show_student_message_root, value='')
            python_grade = StringVar(show_student_message_root, value='')
            modian_grade = StringVar(show_student_message_root, value='')
            shudian_grade = StringVar(show_student_message_root, value='')
            db,cur = Sql_link()
            student_id = eval(entrystudent_id.get())
            #查找ID对应的学生
            sql = "SELECT * FROM student WHERE ID = %d"%(student_id)
            num = cur.execute(sql)
            if(num>0):
                cursor = cur.fetchall()
                for row in cursor:
                    if(student_id==row[0]):
                        student_name1 = row[1]
                        math_grade1 = row[2]
                        python_grade1 = row[3]
                        modian_grade1 = row[4]
                        shudian_grade1 = row[5]
                student_name.set(student_name1)
                math_grade.set(math_grade1)
                python_grade.set(python_grade1)
                modian_grade.set(modian_grade1)
                shudian_grade.set(shudian_grade1)
                # 提示性标签
                labelstudent_name = Label(show_student_message_root, text="学生姓名", font=("微软雅黑 -20"))
                labelmath_grade = Label(show_student_message_root, text="数学成绩", font=("微软雅黑 -20"))
                labelpython_grade = Label(show_student_message_root, text="Python成绩", font=("微软雅黑 -20"))
                labelmodian_grade = Label(show_student_message_root, text="模电成绩", font=("微软雅黑 -20"))
                labelshudian_grade = Label(show_student_message_root, text="数电成绩", font=("微软雅黑 -20"))
                # 设定标签位置
                labelstudent_name.place(x=200, y=100, height=40, width=200)
                labelmath_grade.place(x=200, y=200, height=40, width=200)
                labelpython_grade.place(x=200, y=300, height=40, width=200)
                labelmodian_grade.place(x=200, y=400, height=40, width=200)
                labelshudian_grade.place(x=200, y=500, height=40, width=200)
                #配置文本框数据
                entrystudent_name = Entry((show_student_message_root), textvariable=student_name)
                entrystudent_name.place(x=200, y=150, height=40, width=200)
                entrymath_grade = Entry((show_student_message_root), textvariable=math_grade)
                entrymath_grade.place(x=200, y=250, height=40, width=200)
                entrypython_grade = Entry((show_student_message_root), textvariable=python_grade)
                entrypython_grade.place(x=200, y=350, height=40, width=200)
                entrymodian_grade = Entry((show_student_message_root), textvariable=modian_grade)
                entrymodian_grade.place(x=200, y=450, height=40, width=200)
                entryshudian_grade = Entry((show_student_message_root), textvariable=shudian_grade)
                entryshudian_grade.place(x=200, y=550, height=40, width=200)

                Exit_Button = Button(show_student_message_root, text="退出", font=("微软雅黑 -20"), command=show_student_message_root.destroy)
                Exit_Button.place(x=200, y=630, height=40, width=200)
            else:
                messagebox.showerror(message="改用户不存在!")

        Ok_Button = Button(search_grade_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
        Exit_Button = Button(search_grade_root, text="退出", font=("微软雅黑 -20"), command=search_grade_root.destroy)
        Ok_Button.place(x=75, y=350, height=40, width=200)
        Exit_Button.place(x=325, y=350, height=40, width=200)

学生信息删除

#删除信息
    def delete_data():
        delete_data_root = Tk()
        delete_data_root.title("学生成绩删除")
        delete_data_root.config(width=600)
        delete_data_root.config(height=600)
        # 定义关联变量
        student_id = StringVar(delete_data_root, value='')
        # 提示性标签
        labelstudent_id = Label(delete_data_root, text="学生ID", font=("微软雅黑 -20"))
        # 设定标签位置
        labelstudent_id.place(x=200, y=150, height=40, width=200)
        # 定义录入信息文本框,以关联变量形式存储
        entrystudent_id = Entry(delete_data_root, textvariable=student_id)
        entrystudent_id.place(x=200, y=200, height=40, width=200)
        def Button_Ok():
            db,cur = Sql_link()
            student_id = eval(entrystudent_id.get())
            sql = "DELETE FROM student WHERE ID = %d"%(student_id)
            try:
                cur.execute(sql)
                db.commit()
                messagebox.showinfo(message="删除成功!")
            except:
                messagebox.showerror(message="删除失败!")
        Ok_Button = Button(delete_data_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
        Exit_Button = Button(delete_data_root, text="退出", font=("微软雅黑 -20"), command=delete_data_root.destroy)
        Ok_Button.place(x=75, y=300, height=40, width=200)
        Exit_Button.place(x=325, y=300, height=40, width=200)

单科成绩排序

·这里要用到来进行大量数据的展示,第一个参数是父根对象,然后是数据的数量,再之后是列的名称,后面可以改,当show为'tree headings'时,树对象会显示表头信息,为其他时不显示

·然后我们通过ORDER命令来进行数据的排序

#单科排序
    def sort_one_grade():
        sort_one_grade_root = Tk()
        sort_one_grade_root.title("单科成绩排序")
        sort_one_grade_root.config(width=600)
        sort_one_grade_root.config(height=600)
        #回调函数
        def math_sort():
            sort_all_grade_root = Tk()
            sort_all_grade_root.title("数学成绩排序")
            sort_all_grade_root.config(width=600)
            sort_all_grade_root.config(height=600)
            #创建树对象用于展示数据
            sort_one_grade_tree = ttk.Treeview(sort_all_grade_root, height=30,
                                               columns=['1', '2', '3', '4', '5', '6', '7'],
                                               show='tree headings')
            sort_one_grade_tree.column('1', width=100, anchor='center')
            sort_one_grade_tree.column('2', width=100, anchor='center')
            sort_one_grade_tree.column('3', width=100, anchor='center')
            sort_one_grade_tree.column('4', width=100, anchor='center')
            sort_one_grade_tree.column('5', width=100, anchor='center')
            sort_one_grade_tree.column('6', width=100, anchor='center')
            sort_one_grade_tree.column('7', width=100, anchor='center')
            #设置树的表头
            sort_one_grade_tree.heading('1', text='ID')
            sort_one_grade_tree.heading('2', text='NAME')
            sort_one_grade_tree.heading('3', text='数学成绩')
            sort_one_grade_tree.heading('4', text='Python成绩')
            sort_one_grade_tree.heading('5', text='模电成绩')
            sort_one_grade_tree.heading('6', text='数电成绩')
            sort_one_grade_tree.heading('7', text='总分')
            try:
                db, cur = Sql_link()
                #逆向排序
                sql = "SELECT * FROM student ORDER BY 数学成绩 DESC"
                cur.execute(sql)
                db.commit()
                # 给树各项参数赋值,若为NONE值则break
                for i in range(0, 30):
                    results = cur.fetchone()
                    while (results):
                        sort_one_grade_tree.insert('', i, values=results)
                        break
            except:
                messagebox.showerror(message="排序失败!")
            sort_one_grade_tree.pack()

            Exit_Button = Button(sort_all_grade_root, text="退出", font=("微软雅黑 -20"), command=sort_all_grade_root.destroy)
            Exit_Button.place(x=350, y=530, height=40, width=200)
        def Python_sort():
            sort_all_grade_root = Tk()
            sort_all_grade_root.title("Python成绩排序")
            sort_all_grade_root.config(width=600)
            sort_all_grade_root.config(height=600)
            # 创建树对象用于展示数据
            sort_one_grade_tree = ttk.Treeview(sort_all_grade_root, height=30,
                                               columns=['1', '2', '3', '4', '5', '6', '7'],
                                               show='tree headings')
            sort_one_grade_tree.column('1', width=100, anchor='center')
            sort_one_grade_tree.column('2', width=100, anchor='center')
            sort_one_grade_tree.column('3', width=100, anchor='center')
            sort_one_grade_tree.column('4', width=100, anchor='center')
            sort_one_grade_tree.column('5', width=100, anchor='center')
            sort_one_grade_tree.column('6', width=100, anchor='center')
            sort_one_grade_tree.column('7', width=100, anchor='center')
            # 设置树的表头
            sort_one_grade_tree.heading('1', text='ID')
            sort_one_grade_tree.heading('2', text='NAME')
            sort_one_grade_tree.heading('3', text='数学成绩')
            sort_one_grade_tree.heading('4', text='Python成绩')
            sort_one_grade_tree.heading('5', text='模电成绩')
            sort_one_grade_tree.heading('6', text='数电成绩')
            sort_one_grade_tree.heading('7', text='总分')
            try:
                db, cur = Sql_link()
                sql = "SELECT * FROM student ORDER BY Python成绩 DESC"
                cur.execute(sql)
                db.commit()
                # 给树各项参数赋值,若为NONE值则break
                for i in range(0, 30):
                    results = cur.fetchone()
                    while (results):
                        sort_one_grade_tree.insert('', i, values=results)
                        break
            except:
                messagebox.showerror(message="排序失败!")
            sort_one_grade_tree.pack()

            Exit_Button = Button(sort_all_grade_root, text="退出", font=("微软雅黑 -20"), command=sort_all_grade_root.destroy)
            Exit_Button.place(x=350, y=530, height=40, width=200)
        def modian_sort():
            sort_all_grade_root = Tk()
            sort_all_grade_root.title("模电成绩排序")
            sort_all_grade_root.config(width=600)
            sort_all_grade_root.config(height=600)
            # 创建树对象用于展示数据
            sort_one_grade_tree = ttk.Treeview(sort_all_grade_root, height=30,
                                               columns=['1', '2', '3', '4', '5', '6', '7'],
                                               show='tree headings')
            sort_one_grade_tree.column('1', width=100, anchor='center')
            sort_one_grade_tree.column('2', width=100, anchor='center')
            sort_one_grade_tree.column('3', width=100, anchor='center')
            sort_one_grade_tree.column('4', width=100, anchor='center')
            sort_one_grade_tree.column('5', width=100, anchor='center')
            sort_one_grade_tree.column('6', width=100, anchor='center')
            sort_one_grade_tree.column('7', width=100, anchor='center')
            # 设置树的表头
            sort_one_grade_tree.heading('1', text='ID')
            sort_one_grade_tree.heading('2', text='NAME')
            sort_one_grade_tree.heading('3', text='数学成绩')
            sort_one_grade_tree.heading('4', text='Python成绩')
            sort_one_grade_tree.heading('5', text='模电成绩')
            sort_one_grade_tree.heading('6', text='数电成绩')
            sort_one_grade_tree.heading('7', text='总分')
            try:
                db, cur = Sql_link()
                sql = "SELECT * FROM student ORDER BY 模电成绩 DESC"
                cur.execute(sql)
                db.commit()
                # 给树各项参数赋值,若为NONE值则break
                for i in range(0, 30):
                    results = cur.fetchone()
                    while (results):
                        sort_one_grade_tree.insert('', i, values=results)
                        break
            except:
                messagebox.showerror(message="排序失败!")
            sort_one_grade_tree.pack()
        
            Exit_Button = Button(sort_all_grade_root, text="退出", font=("微软雅黑 -20"), command=sort_all_grade_root.destroy)
            Exit_Button.place(x=350, y=530, height=40, width=200)
        def shudian_sort():
            sort_all_grade_root = Tk()
            sort_all_grade_root.title("数电成绩排序")
            sort_all_grade_root.config(width=600)
            sort_all_grade_root.config(height=600)
            # 创建树对象用于展示数据
            sort_one_grade_tree = ttk.Treeview(sort_all_grade_root, height=30,
                                               columns=['1', '2', '3', '4', '5', '6', '7'],
                                               show='tree headings')
            sort_one_grade_tree.column('1', width=100, anchor='center')
            sort_one_grade_tree.column('2', width=100, anchor='center')
            sort_one_grade_tree.column('3', width=100, anchor='center')
            sort_one_grade_tree.column('4', width=100, anchor='center')
            sort_one_grade_tree.column('5', width=100, anchor='center')
            sort_one_grade_tree.column('6', width=100, anchor='center')
            sort_one_grade_tree.column('7', width=100, anchor='center')
            # 设置树的表头
            sort_one_grade_tree.heading('1', text='ID')
            sort_one_grade_tree.heading('2', text='NAME')
            sort_one_grade_tree.heading('3', text='数学成绩')
            sort_one_grade_tree.heading('4', text='Python成绩')
            sort_one_grade_tree.heading('5', text='模电成绩')
            sort_one_grade_tree.heading('6', text='数电成绩')
            sort_one_grade_tree.heading('7', text='总分')
            try:
                db, cur = Sql_link()
                sql = "SELECT * FROM student ORDER BY 数电成绩 DESC"
                cur.execute(sql)
                db.commit()
                # 给树各项参数赋值,若为NONE值则break
                for i in range(0, 30):
                    results = cur.fetchone()
                    while (results):
                        sort_one_grade_tree.insert('', i, values=results)
                        break
            except:
                messagebox.showerror(message="排序失败!")
            sort_one_grade_tree.pack()

            Exit_Button = Button(sort_all_grade_root, text="退出", font=("微软雅黑 -20"), command=sort_all_grade_root.destroy)
            Exit_Button.place(x=350, y=530, height=40, width=200)
        math_sort_button = Button(sort_one_grade_root,text="数学排序", font=("微软雅黑 -20"), command=math_sort)
        Python_sort_button = Button(sort_one_grade_root, text="Python排序", font=("微软雅黑 -20"), command=Python_sort)
        modian_sort_button = Button(sort_one_grade_root, text="模电排序", font=("微软雅黑 -20"), command=modian_sort)
        shudian_sort_button = Button(sort_one_grade_root, text="数电排序", font=("微软雅黑 -20"), command=shudian_sort)

        math_sort_button.place(x=200, y=100, height=40, width=200)
        Python_sort_button.place(x=200, y=200, height=40, width=200)
        modian_sort_button.place(x=200, y=300, height=40, width=200)
        shudian_sort_button.place(x=200, y=400, height=40, width=200)

        Exit_Button = Button(sort_one_grade_root, text="退出", font=("微软雅黑 -20"), command=sort_one_grade_root.destroy)
        Exit_Button.place(x=200, y=500, height=40, width=200)

总分成绩排序

#总分排序
    def sort_all_grade():
        sort_all_grade_root = Tk()
        sort_all_grade_root.title("全科成绩排序")
        sort_all_grade_root.config(width=600)
        sort_all_grade_root.config(height=600)
        # 创建树对象用于展示数据
        sort_one_grade_tree = ttk.Treeview(sort_all_grade_root, height=30, columns=['1', '2', '3', '4', '5', '6', '7'],
                                           show='tree headings')
        sort_one_grade_tree.column('1', width=100, anchor='center')
        sort_one_grade_tree.column('2', width=100, anchor='center')
        sort_one_grade_tree.column('3', width=100, anchor='center')
        sort_one_grade_tree.column('4', width=100, anchor='center')
        sort_one_grade_tree.column('5', width=100, anchor='center')
        sort_one_grade_tree.column('6', width=100, anchor='center')
        sort_one_grade_tree.column('7', width=100, anchor='center')
        # 设置树的表头
        sort_one_grade_tree.heading('1', text='ID')
        sort_one_grade_tree.heading('2', text='NAME')
        sort_one_grade_tree.heading('3', text='数学成绩')
        sort_one_grade_tree.heading('4', text='Python成绩')
        sort_one_grade_tree.heading('5', text='模电成绩')
        sort_one_grade_tree.heading('6', text='数电成绩')
        sort_one_grade_tree.heading('7', text='总分')
        try:
            db,cur = Sql_link()
            sql = "SELECT * FROM student ORDER BY 总分 DESC"
            cur.execute(sql)
            db.commit()
            #给树各项参数赋值,若为NONE值则break
            for i in range(0,30):
                results = cur.fetchone()
                while(results):
                    sort_one_grade_tree.insert('',i,values=results)
                    break
        except:
            messagebox.showerror(message="排序失败!")
        sort_one_grade_tree.pack()

        Exit_Button = Button(sort_all_grade_root, text="退出", font=("微软雅黑 -20"), command=sort_all_grade_root.destroy)
        Exit_Button.place(x=350, y=530, height=40, width=200)

主界面展示

#设置登录界面
root = Tk()
# 禁止最大化按钮(只显示最小化按钮和关闭按钮)
root.resizable(False,False)
root.minsize(600,600) # 最小尺寸
root.maxsize(600,600) # 最大尺寸
root.title("学生信息管理系统")
#设置长宽
root.config(width=600)
root.config(height=600)
# 添加窗口背景图片
canvas = tkinter.Canvas(root,
                        width = 600,  # 指定Canvas组件的宽度
                        height = 600,  # 指定Canvas组件的高度
                        bg = 'white'  # 指定Canvas组件的背景色
                        )
#添加背景图片
image = Image.open('D:\\1.jpg')

im = ImageTk.PhotoImage(image) #用photoImage打开图片
canvas.create_image(200, 170, image=im)  # 使用creat_image将图片添加到Canvas
canvas.pack()#初始化画布完成

main_button1=Button(root,text="管理员用户创建",font=("微软雅黑 -20"),command=create_manager)
main_button2=Button(root,text="管理员密码登录",font=("微软雅黑 -20"),command=load_manager)
main_button3=Button(root,text="管理员信息修改",font=("微软雅黑 -20"),command=change_manager)
main_button1.place(x=200,y=100,height=40,width=200)
main_button2.place(x=200,y=200,height=40,width=200)
main_button3.place(x=200,y=300,height=40,width=200)

Exit_Button = Button(root, text="退出", font=("微软雅黑 -20"), command=root.destroy)
Exit_Button.place(x=200, y=400, height=40, width=200)

root.mainloop()

实验演示

经测试没有任何问题,欢迎读者来和我反馈,毕竟这只是一个速成,感觉我能学到的还有很多QAQ

7af00428f7734cf0af9f0600f1bdb9e8.png

源码

#!/usr/bin/python3

from tkinter import *
from tkinter import messagebox
from PIL import Image,ImageTk
import tkinter
import pymysql
from tkinter import ttk

# 打开数据库连接
def Sql_link():
    #生成数据库
    db = pymysql.connect(host='localhost',user='root',password='321',database='testdb')
    # 使用 cursor() 方法创建一个游标对象
    cursor = db.cursor()
    return db,cursor
#管理员注册
def create_manager():
    #创建窗口
    create_manager_root = Tk()
    create_manager_root.title("管理员用户创建")
    create_manager_root.config(width=600)
    create_manager_root.config(height=600)
    #定义关联变量
    manager_name = StringVar(create_manager_root, value='')
    manager_code = StringVar(create_manager_root, value='')
    #提示性标签
    labelmanager_name = Label(create_manager_root, text="管理员ID注册", font=("微软雅黑 -20"))
    labelmanager_code = Label(create_manager_root, text="管理员密码设置", font=("微软雅黑 -20"))
    #设定标签位置
    labelmanager_name.place(x=200, y=100, height=40, width=200)
    labelmanager_code.place(x=200, y=200, height=40, width=200)
    #定义录入信息文本框,以关联变量形式存储
    entrymanager_name = Entry((create_manager_root), textvariable=manager_name)
    entrymanager_name.place(x=200, y=150, height=40, width=200)
    entrymanager_code = Entry((create_manager_root), textvariable=manager_code)
    entrymanager_code.place(x=200, y=250, height=40, width=200)
    #录入信息回调函数
    def Button_Ok():
        #设置标志位判断是否存在ID重复
        flag = 0
        #连接数据库
        db, cur = Sql_link()
        #将得到的StringVar对象传值回来
        data_manager_name = str(entrymanager_name.get())
        data_manager_code = str(entrymanager_code.get())
        #判断是否ID重复
        search = cur.execute("SELECT * FROM manager WHERE name =  " + data_manager_name + ';')
        if(search > 0):
            flag = 1
        else:
            flag = 0
        if(flag==0):
            try:
                #写入数据
                sql1 = "INSERT INTO manager(name,code)"
                sql1 += "VALUES('%s','%s')"%(data_manager_name,data_manager_code)
                cur.execute(sql1)
                db.commit()
                messagebox.showinfo(title="恭喜",message="注册成功!!!")
                create_manager_root.destroy()
            except:
                messagebox.showerror(message="注册失败!!!")
        else:
              messagebox.showerror("该用户名已注册!!!")
    #确认以及退出按钮
    Ok_Button = Button(create_manager_root,text="确认",font=("微软雅黑 -20"),command=Button_Ok)
    Exit_Button = Button(create_manager_root, text="退出", font=("微软雅黑 -20"), command=create_manager_root.destroy)
    Ok_Button.place(x=75, y=350, height=40, width=200)
    Exit_Button.place(x=325, y=350, height=40, width=200)
#管理员登录
def load_manager():
    # 创建窗口
    load_manager_root = Tk()
    load_manager_root.title("管理员用户创建")
    load_manager_root.config(width=600)
    load_manager_root.config(height=600)
    # 定义关联变量
    manager_name = StringVar(load_manager_root, value='')
    manager_code = StringVar(load_manager_root, value='')
    # 提示性标签
    labelmanager_name = Label(load_manager_root, text="管理员ID", font=("微软雅黑 -20"))
    labelmanager_code = Label(load_manager_root, text="管理员密码", font=("微软雅黑 -20"))
    # 设定标签位置
    labelmanager_name.place(x=200, y=100, height=40, width=200)
    labelmanager_code.place(x=200, y=200, height=40, width=200)
    # 定义录入信息文本框,以关联变量形式存储
    entrymanager_name = Entry((load_manager_root), textvariable=manager_name)
    entrymanager_name.place(x=200, y=150, height=40, width=200)
    entrymanager_code = Entry((load_manager_root), textvariable=manager_code)
    entrymanager_code.place(x=200, y=250, height=40, width=200)
    def Button_Ok():
        #连接数据库
        db,cur = Sql_link()
        # 将得到的StringVar对象传值回来
        data_manager_name = str(entrymanager_name.get())
        data_manager_code = str(entrymanager_code.get())
        #判断ID与密码是否能够匹配上
        sql = "SELECT * FROM manager WHERE name=%s AND code='%s'"%(data_manager_name,data_manager_code)
        search = cur.execute(sql)
        if(search>0):
            messagebox.showinfo(title="恭喜",message="登录成功!!!")
            show_student()
            load_manager_root.destroy()
        else:
            messagebox.showerror(message="该账号不存在!")
    Ok_Button = Button(load_manager_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
    Exit_Button = Button(load_manager_root, text="退出", font=("微软雅黑 -20"), command=load_manager_root.destroy)
    Ok_Button.place(x=75, y=350, height=40, width=200)
    Exit_Button.place(x=325, y=350, height=40, width=200)
#管理员信息修改
def change_manager():
    # 创建窗口
    change_manager_root = Tk()
    change_manager_root.title("管理员信息修改")
    change_manager_root.config(width=600)
    change_manager_root.config(height=600)
    #删除信息
    def delete_manager():
        #创建窗口
        delete_manager_root = Tk()
        delete_manager_root.title("管理员信息删除")
        delete_manager_root.config(width=600)
        delete_manager_root.config(height=600)

        manager_name = StringVar(delete_manager_root, value='')
        # 提示性标签
        labelmanager_name = Label(delete_manager_root, text="要删除的管理员ID", font=("微软雅黑 -20"))
        entrymanager_name = Entry(delete_manager_root, textvariable=manager_name)

        labelmanager_name.place(x=200, y=100, height=40, width=200)
        entrymanager_name.place(x=200, y=200, height=40, width=200)

        def Button_Ok():
            db,cur = Sql_link()
            manager_name = eval(entrymanager_name.get())
            #查找对应的ID号
            sql = "SELECT * FROM manager WHERE name = %s"%(manager_name)
            search = cur.execute(sql)
            if(search>0):
                messagebox.showinfo(message="删除成功!")
                #删除目标行
                sql1 = "DELETE FROM manager WHERE name = %s"%(manager_name)
                cur.execute(sql1)
                db.commit()
            else:
                messagebox.showerror(message="该用户不存在!")
        Ok_Button = Button(delete_manager_root, text="确认", font=("微软雅黑 -20"),command=Button_Ok)
        Exit_Button = Button(delete_manager_root, text="退出", font=("微软雅黑 -20"), command=delete_manager_root.destroy)
        Ok_Button.place(x=75, y=350, height=40, width=200)
        Exit_Button.place(x=325, y=350, height=40, width=200)
    #修改密码
    def change_code():
        #创建窗口
        change_code_root = Tk()
        change_code_root.title("管理员信息删除")
        change_code_root.config(width=600)
        change_code_root.config(height=600)

        manager_name = StringVar(change_code_root, value='')
        old_manager_code = StringVar(change_code_root, value='')

        # 提示性标签
        labelmanager_name = Label(change_code_root, text="要修改的管理员ID", font=("微软雅黑 -20"))
        entrymanager_name = Entry(change_code_root, textvariable=manager_name)
        labelold_manager_code = Label(change_code_root, text="请输入原密码", font=("微软雅黑 -20"))
        entryold_manager_code = Entry(change_code_root, textvariable=old_manager_code)

        labelmanager_name.place(x=200, y=50, height=40, width=200)
        entrymanager_name.place(x=200, y=150, height=40, width=200)
        labelold_manager_code.place(x=200, y=250, height=40, width=200)
        entryold_manager_code.place(x=200, y=350, height=40, width=200)

        def Button_Ok():
            db,cur = Sql_link()
            manager_name = eval(entrymanager_name.get())
            #匹配对应的ID
            sql = "SELECT * FROM manager WHERE name = %s"%(manager_name)
            search = cur.execute(sql)
            if(search>0):
                #创建新窗口
                input_code_root = Tk()
                input_code_root.title("管理员密码修改")
                input_code_root.config(width=600)
                input_code_root.config(height=600)
                #设置关联变量
                new_manager_code = StringVar(input_code_root, value='')
                labelnew_manager_code = Label(input_code_root, text="新的密码", font=("微软雅黑 -20"))
                entrynew_manager_code = Entry(input_code_root, textvariable=new_manager_code)
                #设置提示性标签
                labelnew_manager_code.place(x=200, y=200, height=40, width=200)
                entrynew_manager_code.place(x=200, y=300, height=40, width=200)
                def Button_Ok():
                    try:
                        new_manager_code = entrynew_manager_code.get()
                        messagebox.showinfo(message="修改成功!")
                        #利用UPDATE来修改密码
                        sql1 = "UPDATE manager SET code = %s WHERE name = %s" % (new_manager_code,manager_name)
                        cur.execute(sql1)
                        db.commit()
                    except:
                        messagebox.showerror(message="修改失败!")
                Ok_Button = Button(input_code_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
                Exit_Button = Button(input_code_root, text="退出", font=("微软雅黑 -20"), command=input_code_root.destroy)
                Ok_Button.place(x=75, y=350, height=40, width=200)
                Exit_Button.place(x=325, y=350, height=40, width=200)

            else:
                messagebox.showerror(message="该用户不存在!")
        Ok_Button = Button(change_code_root, text="确认", font=("微软雅黑 -20"),command=Button_Ok)
        Exit_Button = Button(change_code_root, text="退出", font=("微软雅黑 -20"), command=change_code_root.destroy)
        Ok_Button.place(x=75, y=450, height=40, width=200)
        Exit_Button.place(x=325, y=450, height=40, width=200)
    # 设置按钮
    button_stu1 = Button(change_manager_root, text="删除账号", font=("微软雅黑 -20"),command=delete_manager)
    button_stu2 = Button(change_manager_root, text="修改密码", font=("微软雅黑 -20"),command=change_code)
    button_stu1.place(x=200, y=100, height=40, width=200)
    button_stu2.place(x=200, y=200, height=40, width=200)

    Exit_Button = Button(change_manager_root, text="退出", font=("微软雅黑 -20"), command=change_manager_root.destroy)
    Exit_Button.place(x=200, y=300, height=40, width=200)
#展示学生信息
def show_student():
    #创建窗口
    show_student_root = Tk()
    show_student_root.title("录入学生信息")
    show_student_root.config(width=600)
    show_student_root.config(height=600)
    #输入信息
    def input_grade():
        input_grade_root = Tk()
        input_grade_root.title("学生成绩录入")
        input_grade_root.config(width=600)
        input_grade_root.config(height=900)
        # 定义关联变量
        student_id = StringVar(input_grade_root, value='')
        student_name = StringVar(input_grade_root, value='')
        math_grade = StringVar(input_grade_root, value='')
        python_grade = StringVar(input_grade_root, value='')
        modian_grade = StringVar(input_grade_root, value='')
        shudian_grade = StringVar(input_grade_root, value='')
        # 提示性标签
        labelstudent_id = Label(input_grade_root, text="学生ID", font=("微软雅黑 -20"))
        labelstudent_name = Label(input_grade_root, text="学生姓名", font=("微软雅黑 -20"))
        labelmath_grade = Label(input_grade_root, text="数学成绩", font=("微软雅黑 -20"))
        labelpython_grade = Label(input_grade_root, text="Python成绩", font=("微软雅黑 -20"))
        labelmodian_grade = Label(input_grade_root, text="模电成绩", font=("微软雅黑 -20"))
        labelshudian_grade = Label(input_grade_root, text="数电成绩", font=("微软雅黑 -20"))
        # 设定标签位置
        labelstudent_id.place(x=200, y=50, height=40, width=200)
        labelstudent_name.place(x=200, y=150, height=40, width=200)
        labelmath_grade.place(x=200, y=250, height=40, width=200)
        labelpython_grade.place(x=200, y=350, height=40, width=200)
        labelmodian_grade.place(x=200, y=450, height=40, width=200)
        labelshudian_grade.place(x=200, y=550, height=40, width=200)
        # 定义录入信息文本框,以关联变量形式存储
        entrystudent_id = Entry(input_grade_root, textvariable=student_id)
        entrystudent_id.place(x=200, y=100, height=40, width=200)
        entrystudent_name = Entry(input_grade_root, textvariable=student_name)
        entrystudent_name.place(x=200, y=200, height=40, width=200)
        entrymath_grade = Entry(input_grade_root, textvariable=math_grade)
        entrymath_grade.place(x=200, y=300, height=40, width=200)
        entrypython_grade = Entry(input_grade_root, textvariable=python_grade)
        entrypython_grade.place(x=200, y=400, height=40, width=200)
        entrymodian_grade = Entry(input_grade_root, textvariable=modian_grade)
        entrymodian_grade.place(x=200, y=500, height=40, width=200)
        entryshudian_grade = Entry(input_grade_root, textvariable=shudian_grade)
        entryshudian_grade.place(x=200, y=600, height=40, width=200)
        # 确认以及退出按钮
        def Button_Ok():
            #注意数据类型转换
            db,cur = Sql_link()
            student_id = eval(entrystudent_id.get())
            student_name = entrystudent_name.get()
            math_grade = eval(entrymath_grade.get())
            python_grade = eval(entrypython_grade.get())
            modian_grade = eval(entrymodian_grade.get())
            shudian_grade = eval(entryshudian_grade.get())
            try:
                #这里总分通过求和得到结果
                sql = "INSERT INTO student(ID,NAME,数学成绩,Python成绩,模电成绩,数电成绩,总分)"
                sql += "VALUES(%d,'%s',%.2f,%.2f,%.2f,%.2f,%.2f)"%(
                    student_id,student_name,math_grade,python_grade,modian_grade,
                    shudian_grade,math_grade+python_grade+modian_grade+shudian_grade)
                cur.execute(sql)
                db.commit()
                messagebox.showinfo(message="录入信息成功!")
            except:
                messagebox.showerror(message="录入信息失败!请检查是否有ID重复")
        Ok_Button = Button(input_grade_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
        Exit_Button = Button(input_grade_root, text="退出", font=("微软雅黑 -20"), command=input_grade_root.destroy)
        Ok_Button.place(x=75, y=700, height=40, width=200)
        Exit_Button.place(x=325, y=700, height=40, width=200)
    #查询信息
    def search_grade():
        search_grade_root = Tk()
        search_grade_root.title("学生信息查询")
        search_grade_root.config(width=600)
        search_grade_root.config(height=600)
        # 定义关联变量
        student_id = StringVar(search_grade_root, value='')

        # 提示性标签
        labelstudent_id = Label(search_grade_root, text="要查询学生的ID", font=("微软雅黑 -20"))
        # 设定标签位置
        labelstudent_id.place(x=200, y=150, height=40, width=200)
        # 定义录入信息文本框,以关联变量形式存储
        entrystudent_id = Entry(search_grade_root, textvariable=student_id)
        entrystudent_id.place(x=200, y=250, height=40, width=200)
        def Button_Ok():
            show_student_message_root = Tk()
            show_student_message_root.title("学生信息查询")
            show_student_message_root.config(width=600)
            show_student_message_root.config(height=700)
            #创建关联变量
            student_name = StringVar(show_student_message_root, value='')
            math_grade = StringVar(show_student_message_root, value='')
            python_grade = StringVar(show_student_message_root, value='')
            modian_grade = StringVar(show_student_message_root, value='')
            shudian_grade = StringVar(show_student_message_root, value='')
            db,cur = Sql_link()
            student_id = eval(entrystudent_id.get())
            #查找ID对应的学生
            sql = "SELECT * FROM student WHERE ID = %d"%(student_id)
            num = cur.execute(sql)
            if(num>0):
                cursor = cur.fetchall()
                for row in cursor:
                    if(student_id==row[0]):
                        student_name1 = row[1]
                        math_grade1 = row[2]
                        python_grade1 = row[3]
                        modian_grade1 = row[4]
                        shudian_grade1 = row[5]
                student_name.set(student_name1)
                math_grade.set(math_grade1)
                python_grade.set(python_grade1)
                modian_grade.set(modian_grade1)
                shudian_grade.set(shudian_grade1)
                # 提示性标签
                labelstudent_name = Label(show_student_message_root, text="学生姓名", font=("微软雅黑 -20"))
                labelmath_grade = Label(show_student_message_root, text="数学成绩", font=("微软雅黑 -20"))
                labelpython_grade = Label(show_student_message_root, text="Python成绩", font=("微软雅黑 -20"))
                labelmodian_grade = Label(show_student_message_root, text="模电成绩", font=("微软雅黑 -20"))
                labelshudian_grade = Label(show_student_message_root, text="数电成绩", font=("微软雅黑 -20"))
                # 设定标签位置
                labelstudent_name.place(x=200, y=100, height=40, width=200)
                labelmath_grade.place(x=200, y=200, height=40, width=200)
                labelpython_grade.place(x=200, y=300, height=40, width=200)
                labelmodian_grade.place(x=200, y=400, height=40, width=200)
                labelshudian_grade.place(x=200, y=500, height=40, width=200)
                #配置文本框数据
                entrystudent_name = Entry((show_student_message_root), textvariable=student_name)
                entrystudent_name.place(x=200, y=150, height=40, width=200)
                entrymath_grade = Entry((show_student_message_root), textvariable=math_grade)
                entrymath_grade.place(x=200, y=250, height=40, width=200)
                entrypython_grade = Entry((show_student_message_root), textvariable=python_grade)
                entrypython_grade.place(x=200, y=350, height=40, width=200)
                entrymodian_grade = Entry((show_student_message_root), textvariable=modian_grade)
                entrymodian_grade.place(x=200, y=450, height=40, width=200)
                entryshudian_grade = Entry((show_student_message_root), textvariable=shudian_grade)
                entryshudian_grade.place(x=200, y=550, height=40, width=200)

                Exit_Button = Button(show_student_message_root, text="退出", font=("微软雅黑 -20"), command=show_student_message_root.destroy)
                Exit_Button.place(x=200, y=630, height=40, width=200)
            else:
                messagebox.showerror(message="改用户不存在!")

        Ok_Button = Button(search_grade_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
        Exit_Button = Button(search_grade_root, text="退出", font=("微软雅黑 -20"), command=search_grade_root.destroy)
        Ok_Button.place(x=75, y=350, height=40, width=200)
        Exit_Button.place(x=325, y=350, height=40, width=200)
    #删除信息
    def delete_data():
        delete_data_root = Tk()
        delete_data_root.title("学生成绩删除")
        delete_data_root.config(width=600)
        delete_data_root.config(height=600)
        # 定义关联变量
        student_id = StringVar(delete_data_root, value='')
        # 提示性标签
        labelstudent_id = Label(delete_data_root, text="学生ID", font=("微软雅黑 -20"))
        # 设定标签位置
        labelstudent_id.place(x=200, y=150, height=40, width=200)
        # 定义录入信息文本框,以关联变量形式存储
        entrystudent_id = Entry(delete_data_root, textvariable=student_id)
        entrystudent_id.place(x=200, y=200, height=40, width=200)
        def Button_Ok():
            db,cur = Sql_link()
            student_id = eval(entrystudent_id.get())
            sql = "DELETE FROM student WHERE ID = %d"%(student_id)
            try:
                cur.execute(sql)
                db.commit()
                messagebox.showinfo(message="删除成功!")
            except:
                messagebox.showerror(message="删除失败!")
        Ok_Button = Button(delete_data_root, text="确认", font=("微软雅黑 -20"), command=Button_Ok)
        Exit_Button = Button(delete_data_root, text="退出", font=("微软雅黑 -20"), command=delete_data_root.destroy)
        Ok_Button.place(x=75, y=300, height=40, width=200)
        Exit_Button.place(x=325, y=300, height=40, width=200)
    #单科排序
    def sort_one_grade():
        sort_one_grade_root = Tk()
        sort_one_grade_root.title("单科成绩排序")
        sort_one_grade_root.config(width=600)
        sort_one_grade_root.config(height=600)
        #回调函数
        def math_sort():
            sort_all_grade_root = Tk()
            sort_all_grade_root.title("数学成绩排序")
            sort_all_grade_root.config(width=600)
            sort_all_grade_root.config(height=600)
            #创建树对象用于展示数据
            sort_one_grade_tree = ttk.Treeview(sort_all_grade_root, height=30,
                                               columns=['1', '2', '3', '4', '5', '6', '7'],
                                               show='tree headings')
            sort_one_grade_tree.column('1', width=100, anchor='center')
            sort_one_grade_tree.column('2', width=100, anchor='center')
            sort_one_grade_tree.column('3', width=100, anchor='center')
            sort_one_grade_tree.column('4', width=100, anchor='center')
            sort_one_grade_tree.column('5', width=100, anchor='center')
            sort_one_grade_tree.column('6', width=100, anchor='center')
            sort_one_grade_tree.column('7', width=100, anchor='center')
            #设置树的表头
            sort_one_grade_tree.heading('1', text='ID')
            sort_one_grade_tree.heading('2', text='NAME')
            sort_one_grade_tree.heading('3', text='数学成绩')
            sort_one_grade_tree.heading('4', text='Python成绩')
            sort_one_grade_tree.heading('5', text='模电成绩')
            sort_one_grade_tree.heading('6', text='数电成绩')
            sort_one_grade_tree.heading('7', text='总分')
            try:
                db, cur = Sql_link()
                #逆向排序
                sql = "SELECT * FROM student ORDER BY 数学成绩 DESC"
                cur.execute(sql)
                db.commit()
                # 给树各项参数赋值,若为NONE值则break
                for i in range(0, 30):
                    results = cur.fetchone()
                    while (results):
                        sort_one_grade_tree.insert('', i, values=results)
                        break
            except:
                messagebox.showerror(message="排序失败!")
            sort_one_grade_tree.pack()

            Exit_Button = Button(sort_all_grade_root, text="退出", font=("微软雅黑 -20"), command=sort_all_grade_root.destroy)
            Exit_Button.place(x=350, y=530, height=40, width=200)
        def Python_sort():
            sort_all_grade_root = Tk()
            sort_all_grade_root.title("Python成绩排序")
            sort_all_grade_root.config(width=600)
            sort_all_grade_root.config(height=600)
            # 创建树对象用于展示数据
            sort_one_grade_tree = ttk.Treeview(sort_all_grade_root, height=30,
                                               columns=['1', '2', '3', '4', '5', '6', '7'],
                                               show='tree headings')
            sort_one_grade_tree.column('1', width=100, anchor='center')
            sort_one_grade_tree.column('2', width=100, anchor='center')
            sort_one_grade_tree.column('3', width=100, anchor='center')
            sort_one_grade_tree.column('4', width=100, anchor='center')
            sort_one_grade_tree.column('5', width=100, anchor='center')
            sort_one_grade_tree.column('6', width=100, anchor='center')
            sort_one_grade_tree.column('7', width=100, anchor='center')
            # 设置树的表头
            sort_one_grade_tree.heading('1', text='ID')
            sort_one_grade_tree.heading('2', text='NAME')
            sort_one_grade_tree.heading('3', text='数学成绩')
            sort_one_grade_tree.heading('4', text='Python成绩')
            sort_one_grade_tree.heading('5', text='模电成绩')
            sort_one_grade_tree.heading('6', text='数电成绩')
            sort_one_grade_tree.heading('7', text='总分')
            try:
                db, cur = Sql_link()
                sql = "SELECT * FROM student ORDER BY Python成绩 DESC"
                cur.execute(sql)
                db.commit()
                # 给树各项参数赋值,若为NONE值则break
                for i in range(0, 30):
                    results = cur.fetchone()
                    while (results):
                        sort_one_grade_tree.insert('', i, values=results)
                        break
            except:
                messagebox.showerror(message="排序失败!")
            sort_one_grade_tree.pack()

            Exit_Button = Button(sort_all_grade_root, text="退出", font=("微软雅黑 -20"), command=sort_all_grade_root.destroy)
            Exit_Button.place(x=350, y=530, height=40, width=200)
        def modian_sort():
            sort_all_grade_root = Tk()
            sort_all_grade_root.title("模电成绩排序")
            sort_all_grade_root.config(width=600)
            sort_all_grade_root.config(height=600)
            # 创建树对象用于展示数据
            sort_one_grade_tree = ttk.Treeview(sort_all_grade_root, height=30,
                                               columns=['1', '2', '3', '4', '5', '6', '7'],
                                               show='tree headings')
            sort_one_grade_tree.column('1', width=100, anchor='center')
            sort_one_grade_tree.column('2', width=100, anchor='center')
            sort_one_grade_tree.column('3', width=100, anchor='center')
            sort_one_grade_tree.column('4', width=100, anchor='center')
            sort_one_grade_tree.column('5', width=100, anchor='center')
            sort_one_grade_tree.column('6', width=100, anchor='center')
            sort_one_grade_tree.column('7', width=100, anchor='center')
            # 设置树的表头
            sort_one_grade_tree.heading('1', text='ID')
            sort_one_grade_tree.heading('2', text='NAME')
            sort_one_grade_tree.heading('3', text='数学成绩')
            sort_one_grade_tree.heading('4', text='Python成绩')
            sort_one_grade_tree.heading('5', text='模电成绩')
            sort_one_grade_tree.heading('6', text='数电成绩')
            sort_one_grade_tree.heading('7', text='总分')
            try:
                db, cur = Sql_link()
                sql = "SELECT * FROM student ORDER BY 模电成绩 DESC"
                cur.execute(sql)
                db.commit()
                # 给树各项参数赋值,若为NONE值则break
                for i in range(0, 30):
                    results = cur.fetchone()
                    while (results):
                        sort_one_grade_tree.insert('', i, values=results)
                        break
            except:
                messagebox.showerror(message="排序失败!")
            sort_one_grade_tree.pack()

            Exit_Button = Button(sort_all_grade_root, text="退出", font=("微软雅黑 -20"), command=sort_all_grade_root.destroy)
            Exit_Button.place(x=350, y=530, height=40, width=200)
        def shudian_sort():
            sort_all_grade_root = Tk()
            sort_all_grade_root.title("数电成绩排序")
            sort_all_grade_root.config(width=600)
            sort_all_grade_root.config(height=600)
            # 创建树对象用于展示数据
            sort_one_grade_tree = ttk.Treeview(sort_all_grade_root, height=30,
                                               columns=['1', '2', '3', '4', '5', '6', '7'],
                                               show='tree headings')
            sort_one_grade_tree.column('1', width=100, anchor='center')
            sort_one_grade_tree.column('2', width=100, anchor='center')
            sort_one_grade_tree.column('3', width=100, anchor='center')
            sort_one_grade_tree.column('4', width=100, anchor='center')
            sort_one_grade_tree.column('5', width=100, anchor='center')
            sort_one_grade_tree.column('6', width=100, anchor='center')
            sort_one_grade_tree.column('7', width=100, anchor='center')
            # 设置树的表头
            sort_one_grade_tree.heading('1', text='ID')
            sort_one_grade_tree.heading('2', text='NAME')
            sort_one_grade_tree.heading('3', text='数学成绩')
            sort_one_grade_tree.heading('4', text='Python成绩')
            sort_one_grade_tree.heading('5', text='模电成绩')
            sort_one_grade_tree.heading('6', text='数电成绩')
            sort_one_grade_tree.heading('7', text='总分')
            try:
                db, cur = Sql_link()
                sql = "SELECT * FROM student ORDER BY 数电成绩 DESC"
                cur.execute(sql)
                db.commit()
                # 给树各项参数赋值,若为NONE值则break
                for i in range(0, 30):
                    results = cur.fetchone()
                    while (results):
                        sort_one_grade_tree.insert('', i, values=results)
                        break
            except:
                messagebox.showerror(message="排序失败!")
            sort_one_grade_tree.pack()

            Exit_Button = Button(sort_all_grade_root, text="退出", font=("微软雅黑 -20"), command=sort_all_grade_root.destroy)
            Exit_Button.place(x=350, y=530, height=40, width=200)
        math_sort_button = Button(sort_one_grade_root,text="数学排序", font=("微软雅黑 -20"), command=math_sort)
        Python_sort_button = Button(sort_one_grade_root, text="Python排序", font=("微软雅黑 -20"), command=Python_sort)
        modian_sort_button = Button(sort_one_grade_root, text="模电排序", font=("微软雅黑 -20"), command=modian_sort)
        shudian_sort_button = Button(sort_one_grade_root, text="数电排序", font=("微软雅黑 -20"), command=shudian_sort)

        math_sort_button.place(x=200, y=100, height=40, width=200)
        Python_sort_button.place(x=200, y=200, height=40, width=200)
        modian_sort_button.place(x=200, y=300, height=40, width=200)
        shudian_sort_button.place(x=200, y=400, height=40, width=200)

        Exit_Button = Button(sort_one_grade_root, text="退出", font=("微软雅黑 -20"), command=sort_one_grade_root.destroy)
        Exit_Button.place(x=200, y=500, height=40, width=200)
    #总分排序
    def sort_all_grade():
        sort_all_grade_root = Tk()
        sort_all_grade_root.title("全科成绩排序")
        sort_all_grade_root.config(width=600)
        sort_all_grade_root.config(height=600)
        # 创建树对象用于展示数据
        sort_one_grade_tree = ttk.Treeview(sort_all_grade_root, height=30, columns=['1', '2', '3', '4', '5', '6', '7'],
                                           show='tree headings')
        sort_one_grade_tree.column('1', width=100, anchor='center')
        sort_one_grade_tree.column('2', width=100, anchor='center')
        sort_one_grade_tree.column('3', width=100, anchor='center')
        sort_one_grade_tree.column('4', width=100, anchor='center')
        sort_one_grade_tree.column('5', width=100, anchor='center')
        sort_one_grade_tree.column('6', width=100, anchor='center')
        sort_one_grade_tree.column('7', width=100, anchor='center')
        # 设置树的表头
        sort_one_grade_tree.heading('1', text='ID')
        sort_one_grade_tree.heading('2', text='NAME')
        sort_one_grade_tree.heading('3', text='数学成绩')
        sort_one_grade_tree.heading('4', text='Python成绩')
        sort_one_grade_tree.heading('5', text='模电成绩')
        sort_one_grade_tree.heading('6', text='数电成绩')
        sort_one_grade_tree.heading('7', text='总分')
        try:
            db,cur = Sql_link()
            sql = "SELECT * FROM student ORDER BY 总分 DESC"
            cur.execute(sql)
            db.commit()
            #给树各项参数赋值,若为NONE值则break
            for i in range(0,30):
                results = cur.fetchone()
                while(results):
                    sort_one_grade_tree.insert('',i,values=results)
                    break
        except:
            messagebox.showerror(message="排序失败!")
        sort_one_grade_tree.pack()

        Exit_Button = Button(sort_all_grade_root, text="退出", font=("微软雅黑 -20"), command=sort_all_grade_root.destroy)
        Exit_Button.place(x=350, y=530, height=40, width=200)
    #设置按钮
    button_stu1 = Button(show_student_root, text="录入成绩信息", font=("微软雅黑 -20"),command=input_grade)
    button_stu2 = Button(show_student_root, text="查询成绩信息", font=("微软雅黑 -20"),command=search_grade)
    button_stu3 = Button(show_student_root, text="删除成绩信息", font=("微软雅黑 -20"),command=delete_data)
    button_stu4 = Button(show_student_root, text="分科成绩排序", font=("微软雅黑 -20"),command=sort_one_grade)
    button_stu5 = Button(show_student_root, text="总科成绩排序", font=("微软雅黑 -20"),command=sort_all_grade)
    button_stu1.place(x=200, y=30, height=40, width=200)
    button_stu2.place(x=200, y=130, height=40, width=200)
    button_stu3.place(x=200, y=230, height=40, width=200)
    button_stu4.place(x=200, y=330, height=40, width=200)
    button_stu5.place(x=200, y=430, height=40, width=200)

    Exit_Button = Button(show_student_root, text="退出", font=("微软雅黑 -20"), command=show_student_root.destroy)
    Exit_Button.place(x=200, y=530, height=40, width=200)

#设置登录界面
root = Tk()
# 禁止最大化按钮(只显示最小化按钮和关闭按钮)
root.resizable(False,False)
root.minsize(600,600) # 最小尺寸
root.maxsize(600,600) # 最大尺寸
root.title("学生信息管理系统")
#设置长宽
root.config(width=600)
root.config(height=600)
# 添加窗口背景图片
canvas = tkinter.Canvas(root,
                        width = 600,  # 指定Canvas组件的宽度
                        height = 600,  # 指定Canvas组件的高度
                        bg = 'white'  # 指定Canvas组件的背景色
                        )
#添加背景图片
image = Image.open('D:\\1.jpg')

im = ImageTk.PhotoImage(image) #用photoImage打开图片
canvas.create_image(200, 170, image=im)  # 使用creat_image将图片添加到Canvas
canvas.pack()#初始化画布完成

main_button1=Button(root,text="管理员用户创建",font=("微软雅黑 -20"),command=create_manager)
main_button2=Button(root,text="管理员密码登录",font=("微软雅黑 -20"),command=load_manager)
main_button3=Button(root,text="管理员信息修改",font=("微软雅黑 -20"),command=change_manager)
main_button1.place(x=200,y=100,height=40,width=200)
main_button2.place(x=200,y=200,height=40,width=200)
main_button3.place(x=200,y=300,height=40,width=200)

Exit_Button = Button(root, text="退出", font=("微软雅黑 -20"), command=root.destroy)
Exit_Button.place(x=200, y=400, height=40, width=200)

root.mainloop()

  • 6
    点赞
  • 56
    收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Keitheasun

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值