学生信息管理系统(Python+PySimpleGUI+MySQL)

吐槽一下

经过一段时间学习pymysql的经历,我深刻的体会到了pymysql的不靠谱之处;

就是在使用int型传参,我写的sql语句中格式化%d了之后,我在要传入的数据传递的每一步的去强制转换了,但是他还是会报错,说我的传入参数是string,他要int,我真笑了,最后把%d换成%s结果可以了,,,然后经过我查资料才了解到pymysql他这个库会去自动匹配类型,那也就是说在使用过程中可以对数据类型的关注可以降低一点,让库来给我解决数据类型不匹配的问题

好了,吐槽就完了,然后就是现在的代码以及内容展示了

可视化展示

登录

在这里会去获取mysql中数据库db1的表user中的数据,然后会进行判断,成立下一步,不成立,会弹出弹窗,进行警告,成立就进入菜单

然后点击左上角的"点击进行操作吧"下面的按钮是假的哈哈哈哈哈哈哈哈哈。

编辑操作

这里删除之后需要关闭系统,可能是因为我这里有缓存的原因

查询操作

在这里去查询所有信息就会返回如图弹窗

按学号查询

按姓名查询

按性别查询

按年龄查询

按专业查询

别问我为什么不在这里去做个选择菜单,那就和性别一样了,懒得再进行加工了

按照班级查询

代码

main文件

python 复制代码
from GUI import GUI

if __name__ == '__main__':
    gui = GUI()
    gui.get_in()

GUI文件

python 复制代码
import PySimpleGUI as sg
from LoginDatabase import LoginDatabase
from Editor import Editor
from Find import Find

sg.theme('BlueMono')


class GUI(object):
    def __init__(self):
        self.db_login_conn = LoginDatabase()
        self.db_login = self.db_login_conn.login_database_connect_gui()
        self.find = Find()
        self.editor = Editor()

    def get_in(self):
        layout = [
            [sg.Image(r'E:\Student_Manager_MySQL\th.png')],
            [sg.T('账号'), sg.InputText(default_text='1', key='first')],
            [sg.T('密码'), sg.InputText(default_text='1', key='second')],
            [sg.B('登录', key='login', button_color='blue'), sg.B('重置', key='rollback', button_color='blue')]
        ]
        window = sg.Window('登录框', layout, text_justification='center')
        while True:
            event, values = window.read()
            if event is None:
                break
            if event == 'login':
                sg.popup_auto_close('进入检测中,请等待')

                if values['first'] == self.db_login[0] and values['second'] == self.db_login[1]:
                    sg.popup_auto_close("账号密码正确,请做好准备,即将进入系统------3 2 1 ")
                    self.menu()
                else:
                    sg.popup_auto_close('输入账号或密码错误,你到底是谁,再错我可要报警了奥')
            if event == 'rollback':
                window['first'].update('')
                window['second'].update('')
        window.close()

    def menu(self):
        menu_def = [
            ['点击进行操作吧', ['添加学生', '删除学生', '修改学生', '查询学生']]
        ]
        layout = [
            [sg.Image(r'E:\Student_Manager_MySQL\th.png')],
            [sg.T('您好,欢迎您使用本系统,请进行操作', size=(20, 5), font=('宋体', 15))],
            [sg.Menu(menu_def, key='manage_student', size=(20, 20), font=(15, 20))],
            [sg.B('点击进入系统', size=(10, 5), button_color='blue', key='点击进入系统'),
             sg.B('点击退出系统', size=(10, 5), button_color='red', key='点击退出系统')]
        ]
        window = sg.Window('菜单', layout=layout)
        while True:
            event, values = window.read()
            if event in (None, '退出'):
                break
            if event == '添加学生':
                sg.popup_auto_close('你就添加吧,等一会再告诉你怎么写')
                self.add_student_gui()
            if event == '查询学生':
                self.find_student_gui()
            if event == '修改学生':
                self.change_student_gui()
            if event == '删除学生':
                self.delete_student_gui()
            if event == '点击进入系统':
                sg.popup_auto_close('被骗了吧,哈哈哈哈哈', button_color='red')
            if event == '点击退出系统':
                sg.popup_auto_close('这是真的退出了')
                break
        window.close()

    # 添加学生信息
    def add_student_gui(self):
        layout = [
            [sg.T("姓名"), sg.In(default_text='请不要设置过长', key='-name-')],
            [sg.T("性别"), sg.OptionMenu(values=['男', '女'], key='-gender-')],
            [sg.T("年龄"), sg.In(default_text='请输入整数(必须输入内容)', key='-age-')],
            [sg.T("专业"), sg.OptionMenu(values=['软件工程', '物联网工程', '大数据', '网络工程'], key='-major-')],
            [sg.T("班级"), sg.Combo(values=['1班', '2班'], key='-classroom-')],
            [sg.B(button_text="添加", key='-add-'), sg.B(button_text="退出", key='-exit-')]
        ]
        window = sg.Window(title='添加学生', layout=layout)
        while True:
            event, values = window.read()
            if event is None:
                break
            if event == '-exit-':
                break
            if event == '-add-':
                # print(int(values['-age-']))
                # print(type(int(values['-age-'])))
                (self.editor.insert_student
                 (values['-name-'], values['-gender-'],
                  values['-classroom-'], int(values['-age-']), values['-major-']))
                sg.popup_auto_close('添加成功')
                break
        window.close()

    # 查询学生信息
    def find_student_gui(self):
        layout = [
            [sg.B('按照学号查询', key='find_id')],
            [sg.B('按照姓名查询', key='find_name')],
            [sg.B('按照性别查询', key='find_gender')],
            [sg.B('按照年龄查询', key='find_age')],
            [sg.B('按照专业查询', key='find_major')],
            [sg.B('按照班级查询', key='find_classroom')],
            [sg.B('查询所有信息', key='find_all')]
        ]
        window = sg.Window('查询学生', layout)
        while True:
            event, values = window.read()
            if event is None:
                break
            if event == 'find_id':
                layout_id = [
                    [sg.T('请输入要查询的学生学号id'), sg.In('', key='id')],
                    [sg.B('确认', key='id_ok')]
                ]
                window_id = sg.Window('查询学生id', layout_id)
                while True:
                    event_id, values_id = window_id.read()
                    if event_id is None:
                        break
                    if event_id == 'id_ok':
                        sg.popup(self.find.find_id(int(values_id['id'])))
                window_id.close()
            if event == 'find_name':
                layout_name = [
                    [sg.T('请输入要查询的学生姓名name'), sg.In('', key='name')],
                    [sg.B('确认', key='name_ok')]
                ]
                window_name = sg.Window('查询学生姓名name', layout_name)
                while True:
                    event_name, values_name = window_name.read()
                    if event_name is None:
                        break
                    if event_name == 'name_ok':
                        print(values_name['name'])
                        sg.popup(self.find.find_name(values_name['name']))
                window_name.close()
            if event == 'find_gender':
                layout_gender = [
                    [sg.T('请输入要查询的学生性别gender'), sg.In('', key='gender')],
                    [sg.B('确认', key='gender_ok')]
                ]
                window_gender = sg.Window('查询学生性别gender', layout_gender)
                while True:
                    event_gender, values_gender = window_gender.read()
                    if event_gender is None:
                        break
                    if event_gender == 'gender_ok':
                        print(values_gender['gender'])
                        sg.popup(self.find.find_gender(str(values_gender['gender'])))
                window_gender.close()
            if event == 'find_age':
                layout_age = [
                    [sg.T('请输入要查询的学生年龄age'), sg.In('', key='age')],
                    [sg.B('确认', key='age_ok')]
                ]
                window_age = sg.Window('查询学生年龄age', layout_age)
                while True:
                    event_age, values_age = window_age.read()
                    if event_age is None:
                        break
                    if event_age == 'age_ok':
                        print(values_age['age'])
                        sg.popup(self.find.find_age(int(values_age['age'])))
                window_age.close()
            if event == 'find_major':
                layout_major = [
                    [sg.T('请输入要查询的学生专业major'), sg.In('', key='major')],
                    [sg.B('确认', key='major_ok')]
                ]
                window_major = sg.Window('查询学生专业major', layout_major)
                while True:
                    event_major, values_major = window_major.read()
                    if event_major is None:
                        break
                    if event_major == 'major_ok':
                        print(values_major['major'])
                        sg.popup(self.find.find_major(str(values_major['major'])))
                window_major.close()
            if event == 'find_classroom':
                layout_classroom = [
                    [sg.T('请输入要查询的学生班级classroom'), sg.In('', key='classroom')],
                    [sg.B('确认', key='classroom_ok')]
                ]
                window_classroom = sg.Window('查询学生班级classroom', layout_classroom)
                while True:
                    event_classroom, values_classroom = window_classroom.read()
                    if event_classroom is None:
                        break
                    if event_classroom == 'classroom_ok':
                        sg.popup(self.find.find_classroom(str(values_classroom['classroom'])))
                window_classroom.close()
            if event == 'find_all':
                result = self.find.get_all_students()
                sg.Popup(result)
        window.close()

    # 删除学生
    def delete_student_gui(self):
        layout = [
            [sg.T('请输入要删除的学生的学号'), sg.In('', key='id')],
            [sg.B('确认'), sg.B('取消')]
        ]
        window = sg.Window('删除学生', layout)
        while True:
            event, values = window.read()
            if event is None:
                break
            if event == '确认':
                self.editor.delete_student(int(values['id']))
                sg.popup_auto_close('删除成功')
                break
            if event == '取消':
                break
        window.close()

    # 修改学生信息
    def change_student_gui(self):
        layout = [
            [sg.T('请输入要修改的学生的学号'), sg.In(key='change_id')],
            [sg.B('确认'), sg.B('取消')]
        ]
        window = sg.Window('修改学生', layout)
        while True:
            event, values = window.read()
            if event is None:
                break
            if event == '确认':
                self.find.find_id(values['change_id'])
                name = self.find.find_id(int(values['change_id']))[0]
                gender = self.find.find_id(values['change_id'])[1]
                classroom = self.find.find_id(values['change_id'])[2]
                age = self.find.find_id(values['change_id'])[3]
                major = self.find.find_id(values['change_id'])[4]
                layout_change = [
                    [sg.T('姓名'), sg.In(default_text=name, key='change_name')],
                    [sg.T('性别'), sg.In(default_text=gender, key='change_gender')],
                    [sg.T('班级'), sg.In(default_text=classroom, key='change_classroom')],
                    [sg.T('年龄'), sg.In(default_text=age, key='change_age')],
                    [sg.T('专业'), sg.In(default_text=major, key='change_major')],
                    [sg.B('修改', key='change_ok')]
                ]
                window_change = sg.Window(title='修改框', layout=layout_change)
                while True:
                    event_change, values_change = window_change.read()
                    if event_change is None:
                        break
                    if event_change == 'change_ok':
                        self.editor.update_student(student_id=values['change_id'], name=values_change['change_name'],
                                                   gender=values_change['change_gender'],
                                                   classroom=values_change['change_classroom'],
                                                   age=values_change['change_age'], major=values_change['change_major'])
                        sg.popup_auto_close('更新成功')
                        break
                window_change.close()
            if event == '取消':
                break
            window.close()

DatabaseConnect文件

python 复制代码
import pymysql
import json


class DatabaseConnect(object):
    def __init__(self):
        # 选择json文件进行存储必需数据
        with open(r'E:\Student_Manager_MySQL\database_login.json', 'r') as f:
            db_config = json.load(f)
            self.host = db_config['host']
            self.port = db_config['port']
            self.username = db_config['username']
            self.password = db_config['password']
            self.database = db_config['database']
            self.charset = db_config['charset']

    def connect_database(self):
        try:
            db = pymysql.connect(
                host=self.host,
                port=self.port,
                user=self.username,
                password=self.password,
                database=self.database,
                charset=self.charset
            )
            # print('数据库连接成功')
            # 返回db
            return db
        except pymysql.Error as e:
            print('数据库连接失败' + str(e))

LoginDatabase文件

python 复制代码
from DatabaseConnect import DatabaseConnect


class LoginDatabase(object):
    def __init__(self):
        # 创建database_connect()类的对象
        self.db_connect = DatabaseConnect()
        # 给self.db赋值为db
        self.db = self.db_connect.connect_database()

    def login_database_connect_gui(self):
        # 创建游标对象
        cur = self.db.cursor()
        # 写sql语句
        sqlquery = 'select * from user'
        # 执行SQL语句
        cur.execute(sqlquery)
        # 获取SQL语句的返回值
        results = list(cur.fetchall())
        # print(results)
        result = []
        for i in results:
            for j in i:
                result.append(j)
        # print(result)
        return result
        # print(results)
#
#
# db_connect = database_login()
# db_connect.login_database_connect_gui()

Editor文件

python 复制代码
from LoginDatabase import LoginDatabase


class Editor(LoginDatabase):
    def __init__(self):
        super().__init__()

    # 添加学生
    def insert_student(self, name, gender, classroom, age, major):
        # try:
        cur = self.db.cursor()
        values = (name, int(age), major, gender, classroom)
        sql = "INSERT INTO students (name, age, major, gender, classroom) VALUES (%s, %s, %s, %s, %s)"
        cur.execute(sql, values)
        self.db.commit()
        # xiube = '你回去检查一下你输入的年龄'

    # except Exception as e:
    # return xiube

    # 修改学生
    def update_student(self, student_id, name, age, classroom, gender, major):
        cur = self.db.cursor()
        sql = "UPDATE students SET name = %s, gender = %s, age = %s, major = %s, classroom = %s WHERE id = %s"
        values = (name, gender, age, major, classroom, student_id)
        cur.execute(sql, values)
        self.db.commit()

    # 删除学生
    def delete_student(self, student_id):
        cur = self.db.cursor()
        sql = "DELETE FROM students WHERE id = %s"
        cur.execute(sql, (student_id,))
        self.db.commit()

Find文件

python 复制代码
from LoginDatabase import LoginDatabase


# 查询信息
class Find(LoginDatabase):
    def __init__(self):
        super().__init__()

    # 访问所有学生
    def get_all_students(self):
        cur = self.db.cursor()
        cur.execute("SELECT * FROM students")
        results = list(cur.fetchall())
        print(results)
        return results

    # 访问id
    def find_id(self, student_id):
        cur = self.db.cursor()
        sql = 'select name,gender,classroom,age,major from students where id=%s ' % student_id
        cur.execute(sql)
        list_1 = []
        result = list(cur.fetchall())
        for i in result:
            for j in i:
                list_1.append(j)
        return list_1

    # 访问姓名
    def find_name(self, name):
        cur = self.db.cursor()
        sql = 'select * from students where name like %s'
        cur.execute(sql, (f'%{name}%',))
        result = cur.fetchall()
        return result

    # 访问性别
    def find_gender(self, gender):
        cur = self.db.cursor()
        sql = 'select * from students where gender=%s'
        cur.execute(sql, gender)
        result = cur.fetchall()
        return result

    # 访问班级
    def find_classroom(self, classroom):
        cur = self.db.cursor()
        sql = 'select * from students where classroom like %s'
        cur.execute(sql, (f"%{classroom}%",))
        result = cur.fetchall()
        return result

    # 访问年龄
    def find_age(self, age):
        cur = self.db.cursor()
        sql = 'select * from students where age=%d' % age
        cur.execute(sql)
        result = cur.fetchall()
        return result

    # 访问专业
    def find_major(self, major):
        cur = self.db.cursor()
        sql = 'select * from students where major=%s'
        cur.execute(sql, major)
        result = cur.fetchall()
        return result

json文件

python 复制代码
{
  "host": "localhost",
  "port": 3306,
  "username": "root",
  "password": "1234",
  "database": "db1",
  "charset": "utf8mb4"
}
# 如果要用json那就要把用户名username改成你的用户名,密码和数据库相应也要改成你的

关于可视化中图片

可视化界面中的图片是从网络找的,当然末尾也是找的;

结尾

好了,就到这里了,这一个版本的系统需求基本实现了,缺少一个用户类,然后细分管理员以及普通用户,但是系统功能都实现了,没关系,版本迭代是迟早的,如果有需要可以私信我要源码,如果之后版本迭代的系统我也写出来了,那当然可以给你了。

如有高见,请指教,如若有所收获,还是感激不尽。

灰太狼的大表哥-CSDN博客感谢灰太郎的大表哥给予我的一些关于pymysql的指导。

相关推荐
lwprain9 分钟前
常用docker应用部署,wordpress、mysql、tomcat、nginx、redis
mysql·docker·tomcat
Viktor_Ye16 分钟前
高效集成易快报与金蝶应付单的方案
java·前端·数据库
努力算法的小明1 小时前
SQL 复杂查询
数据库·sql
斗-匕1 小时前
MySQL 三大日志详解
数据库·mysql·oracle
代码中の快捷键1 小时前
MySQL数据库存储引擎
数据库·mysql
只因在人海中多看了你一眼1 小时前
数据库体系
数据库
尘浮生1 小时前
Java项目实战II基于微信小程序的电影院买票选座系统(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven·intellij-idea
六月闻君2 小时前
MySQL 报错:1137 - Can‘t reopen table
数据库·mysql
SelectDB技术团队2 小时前
兼顾高性能与低成本,浅析 Apache Doris 异步物化视图原理及典型场景
大数据·数据库·数据仓库·数据分析·doris
inventecsh2 小时前
mongodb基础操作
数据库·mongodb