Python+tkinter 简易学生信息管理系统

  笔者基于tkinter和pymysql开发出一个简易的学生管理系统。为了方便处理,使用第三方工具navicat连接MySQL,建立数据库及学生信息表。

1.连接数据库

  首先使用第三方工具Navicat连接MySQL,如下图所示。
在这里插入图片描述
  接着输入连接名(随意取,这里笔者取的是linkRPC)、主机、端口(默认3306)、用户名(默认root)以及密码,接着点击左下角的“测试连接”按钮,查看是否连接成功。
在这里插入图片描述

2.创建student表

  本项目中笔者创建studentInfo数据库及student表。表的设计如下图所示。
在这里插入图片描述

3.python连接MySQL数据库

  代码如下:

import pymysql

'''连接数据库'''
def linkDB():
    # 打开数据库连接
    db = pymysql.connect(host='127.0.0.1',
                         port=3306,
                         user='root',
                         passwd='123456',
                         db='studentinfo')

    # 使用 cursor() 方法创建一个游标对象cur
    cur = db.cursor(cursor=pymysql.cursors.DictCursor)

    return db,cur

4.主页设计

  首先展示最终的效果,如下图所示。
在这里插入图片描述
  主页有左上角的菜单,并有增加、删除、查询和修改子功能,主页中央有4个按钮,分别实现学生信息的增删查改4个子页面的跳转。
  实现代码如下:

  1. 设置窗口大小以及背景图片:
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('F:\\1.jpg')

im = ImageTk.PhotoImage(image) #用photoImage打开图片
canvas.create_image(200, 170, image=im)  # 使用creat_image将图片添加到Canvas
canvas.pack()
  1. 添加菜单及功能按钮
#创建顶级菜单及其下拉菜单
menubar=Menu(root)
filemenu=Menu(menubar,tearoff=False)
filemenu.add_command(label="增加",command=insert_stu)
filemenu.add_command(label="删除",command=delete_stu)#command接删除函数/下面接修改函数
filemenu.add_command(label="修改",command=change_stu)
filemenu.add_command(label="查询",command=sel_stu)

filemenu.add_separator()
filemenu.add_command(label="退出",command=root.destroy)
menubar.add_cascade(label="菜单",menu=filemenu)

#显示菜单
root.config(menu=menubar)

buttoninsert_stu=Button(root,text="录入学生信息",font=("微软雅黑 -20"),command=insert_stu)
#buttoninsert_stu.grid(row=2,column=0)由下面的代码将该代码覆盖,显示的是在界面上的位置
buttoninsert_stu.place(x=200,y=150,height=40,width=200)

buttondelete_stu=Button(root,text="删除学生信息",font=("微软雅黑 -20"),command=delete_stu)
buttondelete_stu.place(x=200,y=250,height=40,width=200)

buttonchange_stu=Button(root,text="修改学生信息",font=("微软雅黑 -20"),command=change_stu)
buttonchange_stu.place(x=200,y=350,height=40,width=200)

buttonsel_stu=Button(root,text="查询学生信息",font=("微软雅黑 -20"),command=sel_stu)
buttonsel_stu.place(x=200,y=450,height=40,width=200)

root.mainloop()

5.录入学生信息

  首先展示页面。
在这里插入图片描述
  代码如下:

#增加学生信息
def insert_stu():  #录入学生信息
    root1=Tk()
    root1.title("录入学生信息")
    root1.config(width=600)
    root1.config(height=600)

    #创建关联字符变量
    varName=StringVar(root1,value='')
    varId=StringVar(root1,value='')
    varClass=StringVar(root1,value='')
    varAge=StringVar(root1,value='')

    #创建标签组件
    label=Label(root1,text="姓名:",font=("微软雅黑 -20"))
    label.place(x=30,y=60,height=40,width=80)

    label=Label(root1,text="学号:",font=("微软雅黑 -20"))
    label.place(x=30,y=110,height=40,width=80)


    label=Label(root1,text="班级:",font=("微软雅黑 -20"))
    label.place(x=30,y=160,height=40,width=80)


    label=Label(root1,text="年龄:",font=("微软雅黑 -20"))
    label.place(x=30,y=210,height=40,width=80)

    #创建文本框组件,同时设置关联的变量
    #    姓名entryName
    #    学号entryId
    #    班级entryClass
    #    年龄entryAge


    entryName=Entry((root1),textvariable=varName)
    entryName.place(x=120,y=60,height=40,width=200)

    entryId=Entry((root1),textvariable=varId)
    entryId.place(x=120,y=110,height=40,width=200)

    entryClass=Entry((root1),textvariable=varClass)
    entryClass.place(x=120,y=160,height=40,width=200)

    entryAge=Entry((root1),textvariable=varAge)
    entryAge.place(x=120,y=210,height=40,width=200)


    def buttonOK():
        i=0
        db, cur = linkDB()

        stu_id = eval(entryId.get())#学号输入
        stu_name =str(entryName.get())#姓名录入
        stu_class =eval(entryClass.get())#班级录入
        stu_age=eval(entryAge.get())#年龄录入

        # 查找完成若有重复的学号,则警告。否则录入数据库
        num = cur.execute("SELECT * from student where StuId =  " + str(stu_id) + ' ;')
        if num > 0:
            i = 1
        else:
            i = 0

        if i==1:
            messagebox.showerror('警告',message='学号重复,请重新输入')
        else:
            try:
                sql1 = "INSERT INTO student(StuId,NAME,CLA,AGE)"
                sql1+="VALUES(%d,'%s',%d,%d)"%(stu_id,stu_name,stu_class,stu_age)
                cur.execute(sql1)
                db.commit()
                messagebox.showinfo(title='恭喜',message='录入成功!')
                root1.destroy()
            except:
                messagebox.showerror('警告',message='未录入成功')


    buttonbuttonOK=Button(root1,text="录入学生信息",font=("微软雅黑 -20"),command=buttonOK)
    buttonbuttonOK.place(x=150,y=300,height=40,width=200)
    def cancel():
        varName.set('')
        varId.set('')
        varClass.set('')
        varAge.set('')


    #取消键
    buttonCancel=Button(root1,text="取消",font=("微软雅黑 -20"),command=cancel)
    buttonCancel.place(x=150,y=350,height=40,width=200)

    #退出键
    buttondel=Button(root1,text="退出",font=("微软雅黑 -20"),command=root1.destroy)
    buttondel.place(x=150,y=400,height=40,width=200)
    root1.mainloop()

6.删除学生信息

  以下是删除学生信息页面:
在这里插入图片描述
  代码如下:

# 实现删除学生信息
def delete_stu():
    root2=Tk()
    root2.title("删除学生信息")
    root2.config(width=600)
    root2.config(height=600)


    #添加窗口背景图片

    #创建标签
    label=Label(root2,text="学号:",font=("微软雅黑 -20"))
    label
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值