当前位置:   article > 正文

python做学生信息管理系统,python编辑学生信息系统_python学生系统管理

python学生系统管理

大家好,本文将围绕基于python的学生信息管理系统参考文献展开说明,python编写学生管理系统,用gui做ui是一个很多人都想弄明白的事情,想搞清楚python学生信息管理系统界面设计需要先了解以下几个事情。

python:pyqt5+mysql=学生信息管理系统(图文并茂,超详细)——登录,注册及找回密码篇


前言

在我的前一篇博客中我使用了Tinker作为基础,但是Tinker的缺点就是太枯燥,在美化方面不是很容易进行,因此这次我想分享一下使用pyqt5制作的学生管理系统。
当然这其实也是我的一个课时作业,在上次的基础上,我们不再使用表格,而是运用大家使用更多的储存数据的方式——mysql数据库,通过调用数据库中的数据来实现我们的增删改查,且带有登录界面的管理系统python简单图案代码


接下来我将与大家分享一下我的学生信息管理系统

一、pyqt5是什么?

在这个博客中讲的十分明白了。不太懂的同学可以去学习了解一下:
链接: pyqt5的介绍及简单用法.
于我而言,pyqt5相比Tinker有着明显的优势,就是它在美化这方面是做的很好的,特别是在添加背景图片方面,我认为pyqt5是更加方便的,反正我是在使用Tinker时添加背景图片时遇到了很多的困难,不知道有无大佬指点一下。
当然pyqt5这个库时需要大家自行下载的,下载方法,我就不做过多讲述了,大家可以百度,方法也不是很难(推荐大家使用镜像网下载,能节省大家蛮多时间的)。

二、代码方面

1.引入库及效果展示

在分享之前我们需要做的最重要的一个准备工作就是下载所需要的库: qtawesome,tkinter,os,time,threading,sys,PyQt5,webbrowser,pymysql等,将这些库下载后,我们就可以开始我们的项目了。

首先先给大家看看整体的效果:

登录界面
注册及找回密码:

注册
找回密码
部分主界面:

主界面
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
看着这些界面再看看我之前用Tinker制作的窗口,这不是天差地别吗?啊哈哈哈,所以在我看来pyqt5的优势是更大的。

2.首先介绍一下登录,注册及密码的找回界面吧

欢迎界面

在点击运行整个程序的时候我也制作了欢迎界面,方法与我的上一篇博客大致一样,在这里我还是将代码贴出来吧,原理代码注释应该能看懂。

  1. #欢迎界面
  2. def showWelcome():
  3. sw = root1.winfo_screenwidth() # 获取屏幕宽度
  4. sh = root1.winfo_screenheight() # 获取屏幕高度r
  5. root1.overrideredirect(True) # 去除窗口边框
  6. root1.attributes("-alpha", 1) # 窗口透明度(1为不透明,0为全透明)
  7. x = (sw - 800) / 2
  8. y = (sh - 450) / 2
  9. root1.geometry("800x450+%d+%d" % (x, y)) # 将窗口置于屏幕中央
  10. if os.path.exists(r'./9.gif'): # 搜索图片文件(只能是gif格式)
  11. bm = PhotoImage(file=r'./9.gif')
  12. lb_welcomelogo = Label(root1, image=bm) # 将图片放置于窗口
  13. lb_welcomelogo.bm = bm
  14. lb_welcomelogo.place(x=-2, y=-2, ) # 设置图片位置
  15. def closeWelcome():
  16. for i in range(2):
  17. time.sleep(1) # 屏幕停留时间
  18. root1.destroy()
登录

效果与上一篇博客也是一样的,停留一定时间自动关闭,然后出现登录界面。
在这里插入图片描述
在登录界面中可以看到并没有窗口的扩大,缩小,关闭等按钮,也没有了窗口的边框。因为我通过如下代码对窗口进行了美化(代码写的非常的乱,请大佬们轻喷,我已经尽力了):

  1. #登录界面
  2. class Login(QWidget):
  3. def __init__(self, mode=0, *args, **kwargs):
  4. super().__init__(*args, **kwargs)
  5. self.mode = mode
  6. self.setWindowTitle('登录界面')
  7. self.resize(800, 450)
  8. self.setFixedSize(self.width(), self.height())
  9. self.setWindowFlags(Qt.WindowCloseButtonHint)
  10. self.setWindowOpacity(0.9) # 设置窗口透明度
  11. self.setWindowFlag(QtCore.Qt.FramelessWindowHint) # 隐藏边框
  12. palette = QPalette()
  13. palette.setBrush(QPalette.Background, QBrush(QPixmap("./1.png")))
  14. self.setPalette(palette)
  15. ###### 设置界面控件
  16. self.verticalLayout = QGridLayout(self)
  17. self.H = QLabel(" ")
  18. self.verticalLayout.addWidget(self.H, 0, 0, 9, 0)
  19. self.h = QPushButton("找回密码->>")
  20. self.verticalLayout.addWidget(self.h, 7, 5)
  21. self.h.setStyleSheet('''
  22. QPushButton{border:none;color:black;}
  23. QPushButton:hover{color:white}
  24. ''')
  25. self.a = QPushButton(qtawesome.icon('fa.user-circle', color='white'),":")
  26. self.verticalLayout.addWidget(self.a, 2, 3, 1 ,2)
  27. self.a.setStyleSheet('''
  28. QPushButton{border:none;color:black;}
  29. QPushButton:hover{color:white}
  30. ''')
  31. self.lineEdit_account = QLineEdit()
  32. self.lineEdit_account.setPlaceholderText("请输入账号")
  33. self.verticalLayout.addWidget(self.lineEdit_account, 2, 4, 1,3)
  34. self.lineEdit_account.setStyleSheet(
  35. '''QLineEdit{
  36. border:1px solid gray;
  37. width:200px;
  38. border-radius:10px;
  39. padding:2px 4px;
  40. }''')
  41. self.left_close = QtWidgets.QPushButton("") # 关闭按钮
  42. self.left_visit = QtWidgets.QPushButton("") # 空白按钮
  43. self.left_mini = QtWidgets.QPushButton("") # 最小化按钮
  44. self.verticalLayout.addWidget(self.left_mini, 0, 6, 1, 1)
  45. self.verticalLayout.addWidget(self.left_close, 0, 8, 1, 1)
  46. self.verticalLayout.addWidget(self.left_visit, 0, 7, 1, 1)
  47. self.left_close.setFixedSize(15, 15) # 设置关闭按钮的大小
  48. self.left_visit.setFixedSize(15, 15) # 设置最大化按钮大小
  49. self.left_mini.setFixedSize(15, 15) # 设置最小化按钮大小
  50. self.left_close.setStyleSheet(
  51. '''QPushButton{background:#F76677;border-radius:5px;}QPushButton:hover{background:red;}''')
  52. self.left_visit.setStyleSheet(
  53. '''QPushButton{background:#F7D674;border-radius:5px;}QPushButton:hover{background:yellow;}''')
  54. self.left_mini.setStyleSheet(
  55. '''QPushButton{background:#6DDF6D;border-radius:5px;}QPushButton:hover{background:green;}''')
  56. self.a1 = QPushButton(qtawesome.icon('fa.lock', color='white'), ":")
  57. self.verticalLayout.addWidget(self.a1, 3, 3, 1, 2)
  58. self.a1.setStyleSheet('''
  59. QPushButton{border:none;color:black;}
  60. QPushButton:hover{color:white}
  61. ''')
  62. self.lineEdit_password = QLineEdit()
  63. self.lineEdit_password.setPlaceholderText("请输入密码")
  64. self.verticalLayout.addWidget(self.lineEdit_password, 3, 4, 1, 3)
  65. self.lineEdit_password.setStyleSheet(
  66. '''QLineEdit{
  67. border:1px solid gray;
  68. width:200px;
  69. border-radius:10px;
  70. padding:2px 4px;
  71. }''')
  72. self.lineEdit_password.setEchoMode(QLineEdit.Password)
  73. self.checkBox_remeberpassword = QCheckBox()
  74. self.checkBox_remeberpassword.setText("记住密码")
  75. self.verticalLayout.addWidget(self.checkBox_remeberpassword, 4, 4, 1, 3)
  76. self.checkBox_remeberpassword.setStyleSheet(
  77. "QCheckBox { color : white; }; QCheckBox::indicator { color:black; }");
  78. self.checkBox_autologin = QtWidgets.QCheckBox()
  79. self.checkBox_autologin.setText("自动登录")
  80. self.verticalLayout.addWidget(self.checkBox_autologin, 4, 5, 1, 3)
  81. self.checkBox_autologin.setStyleSheet(
  82. "QCheckBox { color : white; }; QCheckBox::indicator { color:black; }");
  83. self.pushButton_enter = QPushButton()
  84. self.pushButton_enter.setText("登录")
  85. self.verticalLayout.addWidget(self.pushButton_enter, 5, 4, 1, 3)
  86. self.pushButton_enter.setStyleSheet(
  87. "QPushButton{color:highlight}"
  88. "QPushButton:hover{color:white}"
  89. "QPushButton{background-color:rgb(0,191,255)}"
  90. "QPushButton{border:2px}"
  91. "QPushButton{border-radius:10px}"
  92. "QPushButton{padding:5px 6px}"
  93. "QPushButton{font-size:14pt}")
  94. self.pushButton_quit1 = QPushButton()
  95. self.pushButton_quit1.setText("注册")
  96. self.verticalLayout.addWidget(self.pushButton_quit1, 6, 4, 1, 3)
  97. self.pushButton_quit1.setStyleSheet(
  98. "QPushButton{color:highlight}"
  99. "QPushButton:hover{color:white}"
  100. "QPushButton{background-color:rgb(0,191,255)}"
  101. "QPushButton{border:2px}"
  102. "QPushButton{border-radius:10px}"
  103. "QPushButton{padding:5px 6px}"
  104. "QPushButton{font-size:14pt}")
  105. ###### 绑定按钮事件
  106. self.pushButton_enter.clicked.connect(self.on_pushButton_enter_clicked)
  107. self.left_close.clicked.connect(self.QCoreApplication)
  108. self.pushButton_quit1.clicked.connect(self.on_pushButton_enter_clicked1)
  109. self.left_mini.clicked.connect(self.mini)
  110. self.h.clicked.connect(self.h1)
  111. ####初始化登录信息
  112. self.init_login_info()
  113. ####自动登录
  114. self.timer = QTimer(self)
  115. self.timer.timeout.connect(self.goto_autologin)
  116. self.timer.setSingleShot(True)
  117. self.timer.start(1000)
  118. # 自动登录
  119. def goto_autologin(self):
  120. if self.checkBox_autologin.isChecked() == True and self.mode == 0:
  121. self.on_pushButton_enter_clicked()
  122. def on_pushButton_enter_clicked(self):
  123. # 账号判断
  124. account_dict = {}
  125. f = open("1.txt", 'r+')
  126. for line in f:
  127. (keys, value) = line.strip().split()
  128. account_dict[keys] = value
  129. account1 = self.lineEdit_account.text()
  130. password1 = self.lineEdit_password.text()
  131. account_keys = list(account_dict.keys())
  132. f1 = "2.txt"
  133. with open(f1, "w") as file: # 只需要将之前的”w"改为“a"即可,代表追加内
  134. file.write(account1)
  135. if account1 != "" and password1 != "":
  136. if account1 not in account_keys:
  137. reply1 = QMessageBox.information(self, '登录出错', '用户不存在', QMessageBox.Yes | QMessageBox.No,
  138. QMessageBox.Yes)
  139. elif password1 == account_dict[account1]:
  140. ####### 保存登录信息
  141. self.save_login_info()
  142. # 通过验证,关闭对话框并返回1
  143. self.close()
  144. demo.show()
  145. else:
  146. QMessageBox.information(self, '登录出错', '密码错误', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
  147. else:
  148. QMessageBox.information(self, '错误', '输入不能为空!', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
  149. def on_pushButton_enter_clicked1(self):
  150. w2.show()
  151. def QCoreApplication(self):
  152. login.close()
  153. def mini(self):
  154. login.showMinimized()
  155. def h1(self):
  156. w1.show()
  157. # 保存登录信息
  158. def save_login_info(self):
  159. settings = QSettings("config.ini", QSettings.IniFormat) # 方法1:使用配置文件
  160. # settings = QSettings("mysoft","myapp") #方法2:使用注册表
  161. settings.setValue("account", self.lineEdit_account.text())
  162. settings.setValue("password", self.lineEdit_password.text())
  163. settings.setValue("remeberpassword", self.checkBox_remeberpassword.isChecked())
  164. settings.setValue("autologin", self.checkBox_autologin.isChecked())
  165. # 初始化登录信息
  166. def init_login_info(self):
  167. settings = QSettings("config.ini", QSettings.IniFormat) # 方法1:使用配置文件
  168. the_account = settings.value("account")
  169. the_password = settings.value("password")
  170. the_remeberpassword = settings.value("remeberpassword")
  171. the_autologin = settings.value("autologin")
  172. ########
  173. self.lineEdit_account.setText(the_account)
  174. if the_remeberpassword == "true" or the_remeberpassword == True:
  175. self.checkBox_remeberpassword.setChecked(True)
  176. self.lineEdit_password.setText(the_password)
  177. if the_autologin == "true" or the_autologin == True:
  178. self.checkBox_autologin.setChecked(True)

可以从文中的注释看到,我将窗口框去掉,因此扩大,缩小,关闭按钮也随之消失。取而代之的是右上角的三个小点,这三个小点其实是通过QPushButton按钮的方式进行设置的,对其样式进行设置,从而形成了现在的模样。
然后再对每个按钮绑定clicked.connect的事件,从而实现原来窗口的缩小和关闭功能。
不足与缺点:虽然这样设置的窗口样式确实比之前的好看很多,但是也存在一定的问题:

  1. 这样设置后的窗口是无法移动的且不能进行扩大操作(当时写这个项目的时候我也再网上搜索过类似的方法,给我的启发就是通过鼠标点击事件去移动窗口,但是这大大增加了我的工作量,因此我放弃了这种方式)。
  2. 利用按钮点击事件的方式去关闭该窗口时,我发现虽然窗口确实不见了,但是后台程序却还在,这在之后我们连接数据库的时候造成了巨大的困难,使得程序不能完全关闭,从而断开数据库连接,导致数据库连接过多而崩溃。

由于之后的学习关系,我没有再深入的去寻找这些问题的解决方法,希望能看到这里的大佬提供一些解决方法。
再之后就是登录按钮,输入框的设置了,这些通过QLineEdit,QPushButton进行设置的,具体可以看代码,注释还是比较详细的。
这里我仿照了qq的登录方式去网上查找了一些关于自动登录和保存密码的方式,通过QCheckBox进行复选框的设置。由于时间久远,我已经忘记出处了(侵删)。相关的做法在代码中已经展示了。
在这里我并没有将账号和密码存入到数据库中,因为那个时候的自己并没有想到,啊哈哈哈哈哈哈,因此我通过了一个txt文件来储存。然后通过一系列的if语句去判断账号的正确与否。其他的都是一些样式的美化之类的。
对了,在我登录界面上的ICO图标,我是在 The Icons图标库.中找的,其实现在你也可以去阿里矢量网.上去找,上面的图标更多更好看。

注册

在这里插入图片描述
注册界面的样式与登录界面是差不多的,只是输入框以及内层的判断条件不同。
首先我们会匹配输入的账号名称,在我们存放的txt文件中循环遍历是否存在输入的账号名称,如果存在就报错。其次我们增加了再次输入密码的功能,通过比较两次输入密码是否相同即可实现。大致过程如下:

  1. def on_pushButton_enter_clicked1(self):
  2. account_dict = {}
  3. f = open("1.txt", 'r+')
  4. for line in f:
  5. (keys, value) = line.strip().split()
  6. account_dict[keys] = value
  7. account1 = self.lineEdit_account.text()
  8. password1 = self.lineEdit_password.text()
  9. password2 = self.lineEdit_password1.text()
  10. if account1 != "" and password1 != "" and password2 != "":
  11. if password2 != password1:
  12. QMessageBox.information(self, '错误', '密码输入错误,请重新确认', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
  13. else:
  14. account_keys = list(account_dict.keys())
  15. if account1 not in account_keys:
  16. f = "1.txt"
  17. with open(f, "a") as file: # 只需要将之前的”w"改为“a"即可,代表追加内
  18. file.write(account1 + " " + password1 + "\n")
  19. QMessageBox.information(self, '注册成功', '注册成功!', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
  20. else:
  21. QMessageBox.information(self, '注册失败', '账号已存在!', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
  22. else:
  23. QMessageBox.information(self, '注册失败', '输入不能为空!', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)

代码和注释比较详细。

找回密码

在这里插入图片描述
找回密码的界面的样式与之前的也是差不多的,通过获取输入的账号,然后在我们保存账号信息的txt中去循环遍历找到符合的账号密码,然后显示出来,代码如下:

  1. def on_pushButton_enter_clicked1(self):
  2. account_dict = {}
  3. f = open("1.txt", 'r+')
  4. for line in f:
  5. (keys, value) = line.strip().split()
  6. account_dict[keys] = value
  7. account1 = self.lineEdit_account.text()
  8. if account1 == "" :
  9. QMessageBox.information(self, '注册失败', '输入不能为空!', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
  10. else:
  11. account_keys = list(account_dict.keys())
  12. if account1 not in account_keys:
  13. QMessageBox.information(self, '错误', '不存在该账号', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
  14. else:
  15. QMessageBox.information(self, '密码找回成功', '你的密码为:'+account_dict[account1] , QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)

总结

由于时间的关系,这个项目这次没有一下子写完,先写了登录,注册及密码找回的部分,关注我后续有时间我会尽快更新,将整个项目都分享给大家,。希望我们能够共同努力,早日秃头,啊哈哈哈哈!

最后完整的代码我也上传到我的github上了,自取:
链接: 学生信息管理系统2.0.
由于我是初学者,所以整个代码都写在了一个py文件里,所以看着会十分费劲,而且代码内容可能存在重复,多余,繁琐,望理解,但是对于我来说我能够完整的做完整个项目,我已经十分开心了,请大佬轻喷。当然也希望你们能喜欢我的分享。

另外为了方便起见我也在csdn上上传了我的源码,包括所有的数据,你们也可以去下载: python:pyqt5+mysql=学生信息管理系统.rar.

文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树首页概览427544 人正在系统学习中
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/747050
推荐阅读
相关标签
  

闽ICP备14008679号