当前位置:   article > 正文

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

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

吐槽一下 

        经过一段时间学习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文件

  1. from GUI import GUI
  2. if __name__ == '__main__':
  3. gui = GUI()
  4. gui.get_in()

GUI文件

  1. import PySimpleGUI as sg
  2. from LoginDatabase import LoginDatabase
  3. from Editor import Editor
  4. from Find import Find
  5. sg.theme('BlueMono')
  6. class GUI(object):
  7. def __init__(self):
  8. self.db_login_conn = LoginDatabase()
  9. self.db_login = self.db_login_conn.login_database_connect_gui()
  10. self.find = Find()
  11. self.editor = Editor()
  12. def get_in(self):
  13. layout = [
  14. [sg.Image(r'E:\Student_Manager_MySQL\th.png')],
  15. [sg.T('账号'), sg.InputText(default_text='1', key='first')],
  16. [sg.T('密码'), sg.InputText(default_text='1', key='second')],
  17. [sg.B('登录', key='login', button_color='blue'), sg.B('重置', key='rollback', button_color='blue')]
  18. ]
  19. window = sg.Window('登录框', layout, text_justification='center')
  20. while True:
  21. event, values = window.read()
  22. if event is None:
  23. break
  24. if event == 'login':
  25. sg.popup_auto_close('进入检测中,请等待')
  26. if values['first'] == self.db_login[0] and values['second'] == self.db_login[1]:
  27. sg.popup_auto_close("账号密码正确,请做好准备,即将进入系统——3 2 1 ")
  28. self.menu()
  29. else:
  30. sg.popup_auto_close('输入账号或密码错误,你到底是谁,再错我可要报警了奥')
  31. if event == 'rollback':
  32. window['first'].update('')
  33. window['second'].update('')
  34. window.close()
  35. def menu(self):
  36. menu_def = [
  37. ['点击进行操作吧', ['添加学生', '删除学生', '修改学生', '查询学生']]
  38. ]
  39. layout = [
  40. [sg.Image(r'E:\Student_Manager_MySQL\th.png')],
  41. [sg.T('您好,欢迎您使用本系统,请进行操作', size=(20, 5), font=('宋体', 15))],
  42. [sg.Menu(menu_def, key='manage_student', size=(20, 20), font=(15, 20))],
  43. [sg.B('点击进入系统', size=(10, 5), button_color='blue', key='点击进入系统'),
  44. sg.B('点击退出系统', size=(10, 5), button_color='red', key='点击退出系统')]
  45. ]
  46. window = sg.Window('菜单', layout=layout)
  47. while True:
  48. event, values = window.read()
  49. if event in (None, '退出'):
  50. break
  51. if event == '添加学生':
  52. sg.popup_auto_close('你就添加吧,等一会再告诉你怎么写')
  53. self.add_student_gui()
  54. if event == '查询学生':
  55. self.find_student_gui()
  56. if event == '修改学生':
  57. self.change_student_gui()
  58. if event == '删除学生':
  59. self.delete_student_gui()
  60. if event == '点击进入系统':
  61. sg.popup_auto_close('被骗了吧,哈哈哈哈哈', button_color='red')
  62. if event == '点击退出系统':
  63. sg.popup_auto_close('这是真的退出了')
  64. break
  65. window.close()
  66. # 添加学生信息
  67. def add_student_gui(self):
  68. layout = [
  69. [sg.T("姓名"), sg.In(default_text='请不要设置过长', key='-name-')],
  70. [sg.T("性别"), sg.OptionMenu(values=['男', '女'], key='-gender-')],
  71. [sg.T("年龄"), sg.In(default_text='请输入整数(必须输入内容)', key='-age-')],
  72. [sg.T("专业"), sg.OptionMenu(values=['软件工程', '物联网工程', '大数据', '网络工程'], key='-major-')],
  73. [sg.T("班级"), sg.Combo(values=['1班', '2班'], key='-classroom-')],
  74. [sg.B(button_text="添加", key='-add-'), sg.B(button_text="退出", key='-exit-')]
  75. ]
  76. window = sg.Window(title='添加学生', layout=layout)
  77. while True:
  78. event, values = window.read()
  79. if event is None:
  80. break
  81. if event == '-exit-':
  82. break
  83. if event == '-add-':
  84. # print(int(values['-age-']))
  85. # print(type(int(values['-age-'])))
  86. (self.editor.insert_student
  87. (values['-name-'], values['-gender-'],
  88. values['-classroom-'], int(values['-age-']), values['-major-']))
  89. sg.popup_auto_close('添加成功')
  90. break
  91. window.close()
  92. # 查询学生信息
  93. def find_student_gui(self):
  94. layout = [
  95. [sg.B('按照学号查询', key='find_id')],
  96. [sg.B('按照姓名查询', key='find_name')],
  97. [sg.B('按照性别查询', key='find_gender')],
  98. [sg.B('按照年龄查询', key='find_age')],
  99. [sg.B('按照专业查询', key='find_major')],
  100. [sg.B('按照班级查询', key='find_classroom')],
  101. [sg.B('查询所有信息', key='find_all')]
  102. ]
  103. window = sg.Window('查询学生', layout)
  104. while True:
  105. event, values = window.read()
  106. if event is None:
  107. break
  108. if event == 'find_id':
  109. layout_id = [
  110. [sg.T('请输入要查询的学生学号id'), sg.In('', key='id')],
  111. [sg.B('确认', key='id_ok')]
  112. ]
  113. window_id = sg.Window('查询学生id', layout_id)
  114. while True:
  115. event_id, values_id = window_id.read()
  116. if event_id is None:
  117. break
  118. if event_id == 'id_ok':
  119. sg.popup(self.find.find_id(int(values_id['id'])))
  120. window_id.close()
  121. if event == 'find_name':
  122. layout_name = [
  123. [sg.T('请输入要查询的学生姓名name'), sg.In('', key='name')],
  124. [sg.B('确认', key='name_ok')]
  125. ]
  126. window_name = sg.Window('查询学生姓名name', layout_name)
  127. while True:
  128. event_name, values_name = window_name.read()
  129. if event_name is None:
  130. break
  131. if event_name == 'name_ok':
  132. print(values_name['name'])
  133. sg.popup(self.find.find_name(values_name['name']))
  134. window_name.close()
  135. if event == 'find_gender':
  136. layout_gender = [
  137. [sg.T('请输入要查询的学生性别gender'), sg.In('', key='gender')],
  138. [sg.B('确认', key='gender_ok')]
  139. ]
  140. window_gender = sg.Window('查询学生性别gender', layout_gender)
  141. while True:
  142. event_gender, values_gender = window_gender.read()
  143. if event_gender is None:
  144. break
  145. if event_gender == 'gender_ok':
  146. print(values_gender['gender'])
  147. sg.popup(self.find.find_gender(str(values_gender['gender'])))
  148. window_gender.close()
  149. if event == 'find_age':
  150. layout_age = [
  151. [sg.T('请输入要查询的学生年龄age'), sg.In('', key='age')],
  152. [sg.B('确认', key='age_ok')]
  153. ]
  154. window_age = sg.Window('查询学生年龄age', layout_age)
  155. while True:
  156. event_age, values_age = window_age.read()
  157. if event_age is None:
  158. break
  159. if event_age == 'age_ok':
  160. print(values_age['age'])
  161. sg.popup(self.find.find_age(int(values_age['age'])))
  162. window_age.close()
  163. if event == 'find_major':
  164. layout_major = [
  165. [sg.T('请输入要查询的学生专业major'), sg.In('', key='major')],
  166. [sg.B('确认', key='major_ok')]
  167. ]
  168. window_major = sg.Window('查询学生专业major', layout_major)
  169. while True:
  170. event_major, values_major = window_major.read()
  171. if event_major is None:
  172. break
  173. if event_major == 'major_ok':
  174. print(values_major['major'])
  175. sg.popup(self.find.find_major(str(values_major['major'])))
  176. window_major.close()
  177. if event == 'find_classroom':
  178. layout_classroom = [
  179. [sg.T('请输入要查询的学生班级classroom'), sg.In('', key='classroom')],
  180. [sg.B('确认', key='classroom_ok')]
  181. ]
  182. window_classroom = sg.Window('查询学生班级classroom', layout_classroom)
  183. while True:
  184. event_classroom, values_classroom = window_classroom.read()
  185. if event_classroom is None:
  186. break
  187. if event_classroom == 'classroom_ok':
  188. sg.popup(self.find.find_classroom(str(values_classroom['classroom'])))
  189. window_classroom.close()
  190. if event == 'find_all':
  191. result = self.find.get_all_students()
  192. sg.Popup(result)
  193. window.close()
  194. # 删除学生
  195. def delete_student_gui(self):
  196. layout = [
  197. [sg.T('请输入要删除的学生的学号'), sg.In('', key='id')],
  198. [sg.B('确认'), sg.B('取消')]
  199. ]
  200. window = sg.Window('删除学生', layout)
  201. while True:
  202. event, values = window.read()
  203. if event is None:
  204. break
  205. if event == '确认':
  206. self.editor.delete_student(int(values['id']))
  207. sg.popup_auto_close('删除成功')
  208. break
  209. if event == '取消':
  210. break
  211. window.close()
  212. # 修改学生信息
  213. def change_student_gui(self):
  214. layout = [
  215. [sg.T('请输入要修改的学生的学号'), sg.In(key='change_id')],
  216. [sg.B('确认'), sg.B('取消')]
  217. ]
  218. window = sg.Window('修改学生', layout)
  219. while True:
  220. event, values = window.read()
  221. if event is None:
  222. break
  223. if event == '确认':
  224. self.find.find_id(values['change_id'])
  225. name = self.find.find_id(int(values['change_id']))[0]
  226. gender = self.find.find_id(values['change_id'])[1]
  227. classroom = self.find.find_id(values['change_id'])[2]
  228. age = self.find.find_id(values['change_id'])[3]
  229. major = self.find.find_id(values['change_id'])[4]
  230. layout_change = [
  231. [sg.T('姓名'), sg.In(default_text=name, key='change_name')],
  232. [sg.T('性别'), sg.In(default_text=gender, key='change_gender')],
  233. [sg.T('班级'), sg.In(default_text=classroom, key='change_classroom')],
  234. [sg.T('年龄'), sg.In(default_text=age, key='change_age')],
  235. [sg.T('专业'), sg.In(default_text=major, key='change_major')],
  236. [sg.B('修改', key='change_ok')]
  237. ]
  238. window_change = sg.Window(title='修改框', layout=layout_change)
  239. while True:
  240. event_change, values_change = window_change.read()
  241. if event_change is None:
  242. break
  243. if event_change == 'change_ok':
  244. self.editor.update_student(student_id=values['change_id'], name=values_change['change_name'],
  245. gender=values_change['change_gender'],
  246. classroom=values_change['change_classroom'],
  247. age=values_change['change_age'], major=values_change['change_major'])
  248. sg.popup_auto_close('更新成功')
  249. break
  250. window_change.close()
  251. if event == '取消':
  252. break
  253. window.close()

DatabaseConnect文件

  1. import pymysql
  2. import json
  3. class DatabaseConnect(object):
  4. def __init__(self):
  5. # 选择json文件进行存储必需数据
  6. with open(r'E:\Student_Manager_MySQL\database_login.json', 'r') as f:
  7. db_config = json.load(f)
  8. self.host = db_config['host']
  9. self.port = db_config['port']
  10. self.username = db_config['username']
  11. self.password = db_config['password']
  12. self.database = db_config['database']
  13. self.charset = db_config['charset']
  14. def connect_database(self):
  15. try:
  16. db = pymysql.connect(
  17. host=self.host,
  18. port=self.port,
  19. user=self.username,
  20. password=self.password,
  21. database=self.database,
  22. charset=self.charset
  23. )
  24. # print('数据库连接成功')
  25. # 返回db
  26. return db
  27. except pymysql.Error as e:
  28. print('数据库连接失败' + str(e))

LoginDatabase文件

  1. from DatabaseConnect import DatabaseConnect
  2. class LoginDatabase(object):
  3. def __init__(self):
  4. # 创建database_connect()类的对象
  5. self.db_connect = DatabaseConnect()
  6. # 给self.db赋值为db
  7. self.db = self.db_connect.connect_database()
  8. def login_database_connect_gui(self):
  9. # 创建游标对象
  10. cur = self.db.cursor()
  11. # 写sql语句
  12. sqlquery = 'select * from user'
  13. # 执行SQL语句
  14. cur.execute(sqlquery)
  15. # 获取SQL语句的返回值
  16. results = list(cur.fetchall())
  17. # print(results)
  18. result = []
  19. for i in results:
  20. for j in i:
  21. result.append(j)
  22. # print(result)
  23. return result
  24. # print(results)
  25. #
  26. #
  27. # db_connect = database_login()
  28. # db_connect.login_database_connect_gui()

Editor文件

  1. from LoginDatabase import LoginDatabase
  2. class Editor(LoginDatabase):
  3. def __init__(self):
  4. super().__init__()
  5. # 添加学生
  6. def insert_student(self, name, gender, classroom, age, major):
  7. # try:
  8. cur = self.db.cursor()
  9. values = (name, int(age), major, gender, classroom)
  10. sql = "INSERT INTO students (name, age, major, gender, classroom) VALUES (%s, %s, %s, %s, %s)"
  11. cur.execute(sql, values)
  12. self.db.commit()
  13. # xiube = '你回去检查一下你输入的年龄'
  14. # except Exception as e:
  15. # return xiube
  16. # 修改学生
  17. def update_student(self, student_id, name, age, classroom, gender, major):
  18. cur = self.db.cursor()
  19. sql = "UPDATE students SET name = %s, gender = %s, age = %s, major = %s, classroom = %s WHERE id = %s"
  20. values = (name, gender, age, major, classroom, student_id)
  21. cur.execute(sql, values)
  22. self.db.commit()
  23. # 删除学生
  24. def delete_student(self, student_id):
  25. cur = self.db.cursor()
  26. sql = "DELETE FROM students WHERE id = %s"
  27. cur.execute(sql, (student_id,))
  28. self.db.commit()

Find文件

  1. from LoginDatabase import LoginDatabase
  2. # 查询信息
  3. class Find(LoginDatabase):
  4. def __init__(self):
  5. super().__init__()
  6. # 访问所有学生
  7. def get_all_students(self):
  8. cur = self.db.cursor()
  9. cur.execute("SELECT * FROM students")
  10. results = list(cur.fetchall())
  11. print(results)
  12. return results
  13. # 访问id
  14. def find_id(self, student_id):
  15. cur = self.db.cursor()
  16. sql = 'select name,gender,classroom,age,major from students where id=%s ' % student_id
  17. cur.execute(sql)
  18. list_1 = []
  19. result = list(cur.fetchall())
  20. for i in result:
  21. for j in i:
  22. list_1.append(j)
  23. return list_1
  24. # 访问姓名
  25. def find_name(self, name):
  26. cur = self.db.cursor()
  27. sql = 'select * from students where name like %s'
  28. cur.execute(sql, (f'%{name}%',))
  29. result = cur.fetchall()
  30. return result
  31. # 访问性别
  32. def find_gender(self, gender):
  33. cur = self.db.cursor()
  34. sql = 'select * from students where gender=%s'
  35. cur.execute(sql, gender)
  36. result = cur.fetchall()
  37. return result
  38. # 访问班级
  39. def find_classroom(self, classroom):
  40. cur = self.db.cursor()
  41. sql = 'select * from students where classroom like %s'
  42. cur.execute(sql, (f"%{classroom}%",))
  43. result = cur.fetchall()
  44. return result
  45. # 访问年龄
  46. def find_age(self, age):
  47. cur = self.db.cursor()
  48. sql = 'select * from students where age=%d' % age
  49. cur.execute(sql)
  50. result = cur.fetchall()
  51. return result
  52. # 访问专业
  53. def find_major(self, major):
  54. cur = self.db.cursor()
  55. sql = 'select * from students where major=%s'
  56. cur.execute(sql, major)
  57. result = cur.fetchall()
  58. return result

json文件

  1. {
  2. "host": "localhost",
  3. "port": 3306,
  4. "username": "root",
  5. "password": "1234",
  6. "database": "db1",
  7. "charset": "utf8mb4"
  8. }
  9. # 如果要用json那就要把用户名username改成你的用户名,密码和数据库相应也要改成你的

 关于可视化中图片

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

结尾

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

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

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

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号