python tkinter 学生信息管理系统

初学python,代码写的比较繁杂,系统功能还有完善的空间

系统使用了mysql数据库,数据库:sch,用户名:root ,密码:123456,创建表的语句写在代码里面了

​
import tkinter
import tkinter.messagebox
import re
import pymysql
from tkinter import scrolledtext
import os
from tkinter import *
def window_info(main):
        w = main.winfo_screenwidth()
        h = main.winfo_screenheight()
        x = (w / 2) - 200
        y = (h / 2) - 200
        return (x, y)
def conn():
    con = pymysql.connect("localhost", "root", "root", "sch")
    return con

def cur(connection):
    cur = connection.cursor()
    return cur
def exitsys():
    root.destroy()
def teacherlogin():
    #===============================================================================
    def managerindex():
        def addstudent():
            def addone():
                connection = conn()
                cursor = cur(connection)
                try:
                    cursor.execute('insert into student values(%s,%s,%s,%s,%s)',
                                   (addnameentry.get(), addageentry.get(),
                                    addnoentry.get(), addclassentry.get(),'未注册'))
                    connection.commit()
                except:
                    if addsuccessentry.get() != '':
                        addsuccessentry.delete('0', 'end')
                        addsuccessentry.insert('0', '添加失败!')
                    else:
                        addsuccessentry.insert('0', '添加失败!')
                    connection.rollback()
                    connection.close()
                    cursor.close
                if addsuccessentry.get() != '':
                    addsuccessentry.delete('0', 'end')
                    addsuccessentry.insert('0', '添加成功!')
                else:
                    addsuccessentry.insert('0', '添加成功!')

            def addcancel():
                addnameentry.delete('0', 'end')
                addageentry.delete('0', 'end')
                addnoentry.delete('0', 'end')
                addclassentry.delete('0', 'end')
                addsuccessentry.delete('0', 'end')

            def exit():
                add.destroy()

            add = Toplevel()
            add.title('添加学生信息')
            x, y = window_info(add)
            add.geometry("415x295+%d+%d" % (x, y))
            add['bg'] = 'dodgerblue'

            labelname = tkinter.Label(add, text='添加学生', width=80, bg='dodgerblue')
            labelname.place(x=140, y=50, width=150, height=20)

            labelname = tkinter.Label(add, text='学生名:', width=80)
            labelname.place(x=140, y=80, width=60, height=20)
            addnameentry = tkinter.Entry(add, width=200)
            addnameentry.place(x=195, y=80, width=80, height=20)

            labelage = tkinter.Label(add, text='年  龄:', width=80)
            labelage.place(x=140, y=110, width=60, height=20)
            addageentry = tkinter.Entry(add, width=200)
            addageentry.place(x=195, y=110, width=80, height=20)

            labelno = tkinter.Label(add, text='学 号:', width=80)
            labelno.place(x=140, y=140, width=60, height=20)
            addnoentry = tkinter.Entry(add, width=200)
            addnoentry.place(x=195, y=140, width=80, height=20)

            labelclass = tkinter.Label(add, text='班  级:', width=80)
            labelclass.place(x=140, y=170, width=60, height=20)
            addclassentry = tkinter.Entry(add, width=200)
            addclassentry.place(x=195, y=170, width=80, height=20)

            addsuccessentry = tkinter.Entry(add, width=200, state='normal')
            addsuccessentry.place(x=140, y=200, width=135, height=20)

            buttonadd = tkinter.Button(add, text="添加", command=addone)
            buttonadd.place(x=140, y=230, width=50, height=20)

            buttoncancel = tkinter.Button(add, text="重置", command=addcancel)
            buttoncancel.place(x=220, y=230, width=50, height=20)

            add.mainloop()
            return add
            # ===================================================================================

        def findonestudent():
            def search():
                if textsearch.get('1.0', 'end') != '':
                    textsearch.delete('1.0', 'end')
                else:
                    connection = conn()
                    cursor = cur(connection)
                    try:
                        cursor.execute('select * from student where sno=%s', (entrysearchone.get()))
                        data = list(cursor.fetchone())
                        textsearch.insert('insert', "学生姓名:" + data[0]
                                          + "\n" + "年龄:" + data[1]
                                          + "\n" + "学号" + data[2]
                                          + "\n" + "班级:" + data[3] + "\n\n")
                        connection.commit()
                    except:
                        connection.rollback()
                        connection.close()
                        cursor.close
                connection = conn()
                cursor = cur(connection)
                try:
                    cursor.execute('select * from student where sno=%s', (entrysearchone.get()))
                    data = list(cursor.fetchone())
                    textsearch.insert('insert', "学生姓名:" + data[0]
                                      + "\n" + "年龄:" + data[1]
                                      + "\n" + "学号" + data[2]
                                      + "\n" + "班级:" + data[3] + "\n\n")
                    connection.commit()
                except:
                    connection.rollback()
                    connection.close()
                    cursor.close

            def searchonecancel():
                textsearch.delete('1.0', 'end')

            def searchnocancel():
                entrysearchone.delete('0', 'end')

            def exit():
                findone.destroy()

            findone = Toplevel()
            findone.title('查询学生信息')
            x, y = window_info(findone)
            findone.geometry("415x295+%d+%d" % (x, y))
            findone['bg'] = 'dodgerblue'

            labelname = tkinter.Label(findone, text='请输入要查询学生的学号:', width=80, bg='dodgerblue')
            labelname.place(x=140, y=50, width=140, height=20)

            entrysearchone = tkinter.Entry(findone, width=200)
            entrysearchone.place(x=140, y=80, width=150, height=20)

            buttonsearch = tkinter.Button(findone, text="查找", command=search)
            buttonsearch.place(x=140, y=110, width=50, height=20)

            buttonsearch = tkinter.Button(findone, text="重置", command=searchnocancel)
            buttonsearch.place(x=240, y=110, width=50, height=20)

            textsearch = tkinter.scrolledtext.ScrolledText(findone, width=18, height=6)
            textsearch.place(x=140, y=140)

            buttoncancel = tkinter.Button(findone, text="清空", command=searchonecancel)
            buttoncancel.place(x=190, y=230, width=50, height=20)

            findone.mainloop()

        # ==================================================================================
        def deletestudent():
            def deleteone():
                if deleteoneentry.get() == '':
                    tkinter.messagebox.showerror('error', message="请输入学号!")
                else:
                    if textdelete.get('1.0', 'end') != '':
                        textdelete.delete('1.0', 'end')
                    else:
                        connection = conn()
                        cursor = cur(connection)
                        try:
                            cursor.execute('delete from student where sno=%s', (deleteoneentry.get()))
                            connection.commit()
                            cursor.execute('select * from student')
                            data = list(cursor.fetchall())
                            textdelete.insert('insert', "姓名:\t\t年龄:\t\t学号:\t\t班级:")
                            textdelete.insert('insert', "\n")
                            for i in data:
                                print(i)
                                textdelete.insert('insert', '\t\t'.join(i))
                                textdelete.insert('insert', "\n")
                        except:
                            connection.rollback()
                            connection.close()
                            cursor.close
                    connection = conn()
                    cursor = cur(connection)
                    try:
                        cursor.execute('delete from student where sno=%s', (deleteoneentry.get()))
                        connection.commit()
                        cursor.execute('select * from student')
                        data = list(cursor.fetchall())
                        textdelete.insert('insert', "姓名:\t\t年龄:\t\t学号:\t\t班级:")
                        textdelete.insert('insert', "\n")
                        for i in data:
                            print(i)
                            textdelete.insert('insert', '\t\t'.join(i))
                            textdelete.insert('insert', "\n")
                    except:
                        connection.rollback()
                        connection.close()
                        cursor.close

            def exit():
                deleteone.destroy()

            def deleteonecancel():
                deleteoneentry.delete('0', 'end')

            delete = Toplevel()
            delete.title('删除学生信息')
            x, y = window_info(delete)
            delete.geometry("415x295+%d+%d" % (x, y))
            delete['bg'] = 'dodgerblue'

            labelname = tkinter.Label(delete, text='请输入要删除学生的学号:', bg='dodgerblue')
            labelname.place(x=5, y=20, width=140, height=20)

   
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值