找回密码
 会员注册
查看: 36|回复: 0

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

[复制链接]

2万

主题

0

回帖

6万

积分

超级版主

积分
64756
发表于 2024-9-11 17:53:51 | 显示全部楼层 |阅读模式
吐槽一下         经过一段时间学习pymysql的经历,我深刻的体会到了pymysql的不靠谱之处;        就是在使用int型传参,我写的sql语句中格式化%d了之后,我在要传入的数据传递的每一步的去强制转换了,但是他还是会报错,说我的传入参数是string,他要int,我真笑了,最后把%d换成%s结果可以了,,,然后经过我查资料才了解到pymysql他这个库会去自动匹配类型,那也就是说在使用过程中可以对数据类型的关注可以降低一点,让库来给我解决数据类型不匹配的问题。        好了,吐槽就完了,然后就是现在的代码以及内容展示了可视化展示不足之处1.弹窗过多,看多了会烦2.界面设置大小不够合理3.图片元素设置不够合理4.查询的操作可以使用控件Table展示一个表格,懒的做了登录在这里会去获取mysql中数据库db1的表user中的数据,然后会进行判断,成立下一步,不成立,会弹出弹窗,进行警告,成立就进入菜单然后点击左上角的”点击进行操作吧“下面的按钮是假的哈哈哈哈哈哈哈哈哈。编辑操作 这里删除之后需要关闭系统,可能是因为我这里有缓存的原因查询操作在这里去查询所有信息就会返回如图弹窗按学号查询按姓名查询按性别查询按年龄查询 按专业查询别问我为什么不在这里去做个选择菜单,那就和性别一样了,懒得再进行加工了按照班级查询  代码可提高之处1.pymysql的使用中游标和数据库的未关闭造成的资源浪费,在进行sql语句的执行时,execute执行之后提交事务,然后关闭游标,关闭数据库的连接2.json文件的多次读取,造成了性能的浪费。使用类属性,然后使用静态方法判断属性是否为空,空则读取json文件内容,非空则继续读取,之后再进行连接。3.for循环的不当使用,因为数据库的表中字段并不多,所以我去遍历的行为可以,当字段多了就会对性能造成极大浪费,不过字段毕竟就设置了几个。main文件fromGUIimportGUIif__name__=='__main__':gui=GUI()gui.get_in()GUI文件importPySimpleGUIassgfromLoginDatabaseimportLoginDatabasefromEditorimportEditorfromFindimportFindsg.theme('BlueMono')classGUI(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()defget_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')whileTrue:event,values=window.read()ifeventisNone:breakifevent=='login':sg.popup_auto_close('进入检测中,请等待')ifvalues['first']==self.db_login[0]andvalues['second']==self.db_login[1]:sg.popup_auto_close("账号密码正确,请做好准备,即将进入系统——321")self.menu()else:sg.popup_auto_close('输入账号或密码错误,你到底是谁,再错我可要报警了奥')ifevent=='rollback':window['first'].update('')window['second'].update('')window.close()defmenu(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)whileTrue:event,values=window.read()ifeventin(None,'退出'):breakifevent=='添加学生':sg.popup_auto_close('你就添加吧,等一会再告诉你怎么写')self.add_student_gui()ifevent=='查询学生':self.find_student_gui()ifevent=='修改学生':self.change_student_gui()ifevent=='删除学生':self.delete_student_gui()ifevent=='点击进入系统':sg.popup_auto_close('被骗了吧,哈哈哈哈哈',button_color='red')ifevent=='点击退出系统':sg.popup_auto_close('这是真的退出了')breakwindow.close()#添加学生信息defadd_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)whileTrue:event,values=window.read()ifeventisNone:breakifevent=='-exit-':breakifevent=='-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('添加成功')breakwindow.close()#查询学生信息deffind_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)whileTrue:event,values=window.read()ifeventisNone:breakifevent=='find_id':layout_id=[[sg.T('请输入要查询的学生学号id'),sg.In('',key='id')],[sg.B('确认',key='id_ok')]]window_id=sg.Window('查询学生id',layout_id)whileTrue:event_id,values_id=window_id.read()ifevent_idisNone:breakifevent_id=='id_ok':sg.popup(self.find.find_id(int(values_id['id'])))window_id.close()ifevent=='find_name':layout_name=[[sg.T('请输入要查询的学生姓名name'),sg.In('',key='name')],[sg.B('确认',key='name_ok')]]window_name=sg.Window('查询学生姓名name',layout_name)whileTrue:event_name,values_name=window_name.read()ifevent_nameisNone:breakifevent_name=='name_ok':print(values_name['name'])sg.popup(self.find.find_name(values_name['name']))window_name.close()ifevent=='find_gender':layout_gender=[[sg.T('请输入要查询的学生性别gender'),sg.In('',key='gender')],[sg.B('确认',key='gender_ok')]]window_gender=sg.Window('查询学生性别gender',layout_gender)whileTrue:event_gender,values_gender=window_gender.read()ifevent_genderisNone:breakifevent_gender=='gender_ok':print(values_gender['gender'])sg.popup(self.find.find_gender(str(values_gender['gender'])))window_gender.close()ifevent=='find_age':layout_age=[[sg.T('请输入要查询的学生年龄age'),sg.In('',key='age')],[sg.B('确认',key='age_ok')]]window_age=sg.Window('查询学生年龄age',layout_age)whileTrue:event_age,values_age=window_age.read()ifevent_ageisNone:breakifevent_age=='age_ok':print(values_age['age'])sg.popup(self.find.find_age(int(values_age['age'])))window_age.close()ifevent=='find_major':layout_major=[[sg.T('请输入要查询的学生专业major'),sg.In('',key='major')],[sg.B('确认',key='major_ok')]]window_major=sg.Window('查询学生专业major',layout_major)whileTrue:event_major,values_major=window_major.read()ifevent_majorisNone:breakifevent_major=='major_ok':print(values_major['major'])sg.popup(self.find.find_major(str(values_major['major'])))window_major.close()ifevent=='find_classroom':layout_classroom=[[sg.T('请输入要查询的学生班级classroom'),sg.In('',key='classroom')],[sg.B('确认',key='classroom_ok')]]window_classroom=sg.Window('查询学生班级classroom',layout_classroom)whileTrue:event_classroom,values_classroom=window_classroom.read()ifevent_classroomisNone:breakifevent_classroom=='classroom_ok':sg.popup(self.find.find_classroom(str(values_classroom['classroom'])))window_classroom.close()ifevent=='find_all':result=self.find.get_all_students()sg.Popup(result)window.close()#删除学生defdelete_student_gui(self):layout=[[sg.T('请输入要删除的学生的学号'),sg.In('',key='id')],[sg.B('确认'),sg.B('取消')]]window=sg.Window('删除学生',layout)whileTrue:event,values=window.read()ifeventisNone:breakifevent=='确认':self.editor.delete_student(int(values['id']))sg.popup_auto_close('删除成功')breakifevent=='取消':breakwindow.close()#修改学生信息defchange_student_gui(self):layout=[[sg.T('请输入要修改的学生的学号'),sg.In(key='change_id')],[sg.B('确认'),sg.B('取消')]]window=sg.Window('修改学生',layout)whileTrue:event,values=window.read()ifeventisNone:breakifevent=='确认':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)whileTrue:event_change,values_change=window_change.read()ifevent_changeisNone:breakifevent_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('更新成功')breakwindow_change.close()ifevent=='取消':breakwindow.close()DatabaseConnect文件importpymysqlimportjsonclassDatabaseConnect(object):def__init__(self):#选择json文件进行存储必需数据withopen(r'E:\Student_Manager_MySQL\database_login.json','r')asf: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']defconnect_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('数据库连接成功')#返回dbreturndbexceptpymysql.Errorase:print('数据库连接失败'+str(e))LoginDatabase文件fromDatabaseConnectimportDatabaseConnectclassLoginDatabase(object):def__init__(self):#创建database_connect()类的对象self.db_connect=DatabaseConnect()#给self.db赋值为dbself.db=self.db_connect.connect_database()deflogin_database_connect_gui(self):#创建游标对象cur=self.db.cursor()#写sql语句sqlquery='select*fromuser'#执行SQL语句cur.execute(sqlquery)#获取SQL语句的返回值results=list(cur.fetchall())#print(results)result=[]foriinresults:forjini:result.append(j)#print(result)returnresult#print(results)###db_connect=database_login()#db_connect.login_database_connect_gui()Editor文件fromLoginDatabaseimportLoginDatabaseclassEditor(LoginDatabase):def__init__(self):super().__init__()#添加学生definsert_student(self,name,gender,classroom,age,major):#try:cur=self.db.cursor()values=(name,int(age),major,gender,classroom)sql="INSERTINTOstudents(name,age,major,gender,classroom)VALUES(%s,%s,%s,%s,%s)"cur.execute(sql,values)self.db.commit()#xiube='你回去检查一下你输入的年龄'#exceptExceptionase:#returnxiube#修改学生defupdate_student(self,student_id,name,age,classroom,gender,major):cur=self.db.cursor()sql="UPDATEstudentsSETname=%s,gender=%s,age=%s,major=%s,classroom=%sWHEREid=%s"values=(name,gender,age,major,classroom,student_id)cur.execute(sql,values)self.db.commit()#删除学生defdelete_student(self,student_id):cur=self.db.cursor()sql="DELETEFROMstudentsWHEREid=%s"cur.execute(sql,(student_id,))self.db.commit()Find文件fromLoginDatabaseimportLoginDatabase#查询信息classFind(LoginDatabase):def__init__(self):super().__init__()#访问所有学生defget_all_students(self):cur=self.db.cursor()cur.execute("SELECT*FROMstudents")results=list(cur.fetchall())print(results)returnresults#访问iddeffind_id(self,student_id):cur=self.db.cursor()sql='selectname,gender,classroom,age,majorfromstudentswhereid=%s'%student_idcur.execute(sql)list_1=[]result=list(cur.fetchall())foriinresult:forjini:list_1.append(j)returnlist_1#访问姓名deffind_name(self,name):cur=self.db.cursor()sql='select*fromstudentswherenamelike%s'cur.execute(sql,(f'%{name}%',))result=cur.fetchall()returnresult#访问性别deffind_gender(self,gender):cur=self.db.cursor()sql='select*fromstudentswheregender=%s'cur.execute(sql,gender)result=cur.fetchall()returnresult#访问班级deffind_classroom(self,classroom):cur=self.db.cursor()sql='select*fromstudentswhereclassroomlike%s'cur.execute(sql,(f"%{classroom}%",))result=cur.fetchall()returnresult#访问年龄deffind_age(self,age):cur=self.db.cursor()sql='select*fromstudentswhereage=%d'%agecur.execute(sql)result=cur.fetchall()returnresult#访问专业deffind_major(self,major):cur=self.db.cursor()sql='select*fromstudentswheremajor=%s'cur.execute(sql,major)result=cur.fetchall()returnresultjson文件{"host":"localhost","port":3306,"username":"root","password":"1234","database":"db1","charset":"utf8mb4"}#如果要用json那就要把用户名username改成你的用户名,密码和数据库相应也要改成你的 关于可视化中图片可视化界面中的图片元素的图片是从网络找的,当然末尾也是找的;结尾        好了,就到这里了,这一个版本的系统需求基本实现了,缺少一个用户类,然后细分管理员以及普通用户,但是系统功能都实现了,没关系,版本迭代是迟早的,如果有需要可以私信我要源码,如果之后版本迭代的系统我也写出来了,那当然可以给你了。    如有高见,请指教,如若有所收获,还是感激不尽。灰太狼的大表哥-CSDN博客感谢灰太郎的大表哥给予我的一些关于pymysql的指导。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2024-12-28 21:21 , Processed in 0.574930 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表