软件实习项目4——校园一卡通管理系统(代码实现)
(省去了各UI文件以及其转成的py文件)
一、窗口的互相调用与各种功能的实现
1、main.py
from PyQt5.QtWidgets import QApplication
from BatchInsertedWindow import BatchInsertedWindow
from ForgetWindow import ResetWindow, SecurityQuestionWindow
from LoginWindow import LoginWindow
from MainWindow import MainWindow
from InsertWindow import InsertWindow
import sys
if __name__ == "__main__":
app = QApplication(sys.argv)
login_widget = LoginWindow() # 窗口控件
login_widget.show()
main_widget = MainWindow() # 窗口控件
forget_widget = SecurityQuestionWindow() # 密保窗口
reset_widget = ResetWindow() # 重置密码窗口
insert_widget = InsertWindow() # 窗口控件
batch_insert_widget = BatchInsertedWindow() # 批量插入窗口
login_widget.signal.connect(main_widget.show) # 连接槽函数
login_widget.forget_signal.connect(forget_widget.show) # 显示忘记密码窗口
forget_widget.signal.connect(reset_widget.show) # 显示重置密码窗口
forget_widget.back_signal.connect(login_widget.show) # 显示登录窗口
reset_widget.signal.connect(login_widget.show) # 回到登录窗口
reset_widget.back_signal.connect(login_widget.show) # 回到登录窗口
main_widget.batch_signal.connect(batch_insert_widget.show) # 显示批量插入窗口
main_widget.insert_signal.connect(insert_widget.show) # 连接槽函数
insert_widget.inserted_signal.connect(main_widget.query) # 更新数据
sys.exit(app.exec_())
2、LoginWindow.py
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import ctypes
from PyQt5.QtWidgets import QMainWindow, QLineEdit, QMessageBox, QWidget
from Functions import load
from Login_UI import Ui_LoginWindow
class LoginWindow(QtWidgets.QMainWindow, Ui_LoginWindow):
signal = pyqtSignal()
forget_signal = pyqtSignal()
def __init__(self):
super().__init__()
self.setupUi(self)
self.setWindowTitle('校园一卡通管理系统登录') # 设置窗口标题
self.setWindowIcon(QIcon('logo.png')) # 设置窗口图标
# self.setStyleSheet('QWidget{background-color:%s}' % QColor(222, 222, 222).name()) # 设置窗口背景色
self.setWindowOpacity(0.95) # 设置整个计算器窗口的透明度
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("myappid") # 任务栏图标
self.setWindowFlags(Qt.FramelessWindowHint)
self.show()
self.set_input() # 输入账号和输入密码的设置
# 将信号连接到对应槽函数
self.login_button.clicked.connect(self.username_password)
self.forget.clicked.connect(self.forget_password)
self.login_close.clicked.connect(QCoreApplication.instance().quit)
# 输入账号和输入密码的设置
def set_input(self):
self.input_username.setPlaceholderText("请输入账号") # 灰色的占位符
self.input_password.setPlaceholderText("请输入密码") # 灰色的占位符
self.input_username.setContextMenuPolicy(Qt.NoContextMenu) # 禁止复制粘贴
self.input_password.setContextMenuPolicy(Qt.NoContextMenu) # 禁止复制粘贴
self.input_password.setEchoMode(QLineEdit.Password) # 输入时呈现圆点,不显示密码
# 检查用户名与密码是否匹配
def username_password(self):
if load(self.input_username.text(), self.input_password.text()):
self.hide()
self.signal.emit() # 发射信号
else:
QMessageBox.warning(self, "错误", "账号或密码错误!")
self.input_password.setText('')
pass
# 忘记密码按钮触发的信号
def forget_password(self):
self.hide()
self.forget_signal.emit() # 发射忘记密码信号
# 键盘登录
def keyPressEvent(self, event):
QWidget.keyPressEvent(self, event)
key = event.key()
if key == Qt.Key_Enter:
self.username_password()
3、ForgetWindow.py
包含了密保验证和重置密码两个窗口
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import ctypes
import re
from PyQt5.QtWidgets import QMessageBox, QWidget, QLineEdit
from Functions import SecurityQuestion, ResetPassword
from SecurityQuestion_UI import Ui_SecurityQuestionWindow
from Reset_UI import Ui_ResetWindow
global username_changing
class SecurityQuestionWindow(QtWidgets.QMainWindow, Ui_SecurityQuestionWindow):
signal = pyqtSignal()
back_signal = pyqtSignal()
def __init__(self):
super().__init__()
self.setupUi(self)
self.setWindowTitle('忘记密码') # 设置窗口标题
self.setWindowIcon(QIcon('logo.png')) # 设置窗口图标
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("myappid") # 任务栏图标
self.setWindowFlags(Qt.FramelessWindowHint)
# 将信号连接到对应槽函数
self.confirm_button.clicked.connect(self.security_question)
self.back_button.clicked.connect(self.back_to_login)
self.close_button.clicked.connect(QCoreApplication.instance().quit)
# 检查用户名与密保是否匹配
def security_question(self):
if SecurityQuestion(self.input_username.text(), self.input_security.text()):
global username_changing
username_changing = self.input_username.text()
self.input_username.setText('')
self.input_security.setText('')
self.hide()
self.signal.emit() # 发射信号
else:
QMessageBox.warning(self, "错误", "密保错误!")
self.input_security.setText('')
# 键盘确认
def keyPressEvent(self, event):
QWidget.keyPressEvent(self, event)
key = event.key()
if key == Qt.Key_Enter:
self.security_question()
def back_to_login(self):
self.hide()
self.back_signal.emit() # 触发返回登陆界面的信号
class ResetWindow(QtWidgets.QMainWindow, Ui_ResetWindow):
signal = pyqtSignal()
back_signal = pyqtSignal()
def __init__(self):
super().__init__()
self.setupUi(self)
self.setWindowTitle('重置密码') # 设置窗口标题
self.setWindowIcon(QIcon('logo.png')) # 设置窗口图标
# self.setStyleSheet('QWidget{background-color:%s}' % QColor(222, 222, 222).name()) # 设置窗口背景色
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("myappid") # 任务栏图标
self.setWindowFlags(Qt.FramelessWindowHint)
self.set_input() # 输入密码的设置
# 将信号连接到对应槽函数
self.submit_button.clicked.connect(self.Reset)
self.back_button.clicked.connect(self.back_to_login)
self.close_button.clicked.connect(QCoreApplication.instance().quit)
def set_input(self):
self.input_pass1.setContextMenuPolicy(Qt.NoContextMenu) # 禁止复制粘贴
self.input_pass2.setContextMenuPolicy(Qt.NoContextMenu) # 禁止复制粘贴
self.input_pass1.setEchoMode(QLineEdit.Password) # 输入时呈现圆点,不显示密码
self.input_pass2.setEchoMode(QLineEdit.Password) # 输入时呈现圆点,不显示密码
# 检查用户名与密保是否匹配
def Reset(self):
regx = re.compile(r"^[a-zA-Z]\w{5,14}")
global username_changing
if username_changing != '' and self.input_pass1.text() == self.input_pass2.text() and regx.match(self.input_pass1.text()) is not None:
if regx.match(self.input_pass1.text()).group() == self.input_pass1.text():
ResetPassword(username_changing, self.input_pass1.text())
QMessageBox.information(self, "信息", "修改密码成功!")
self.input_pass1.setText('')
self.input_pass2.setText('')
self.hide()
self.signal.emit() # 发射信号
else:
QMessageBox.warning(self, "警告", "请输入符合条件的密码!")
self.input_pass1.setText('')
self.input_pass2.setText('')
else:
if self.input_pass1.text() != self.input_pass2.text():
QMessageBox.warning(self, "警告", "两次输入的密码不一致,请重新输入!")
else:
QMessageBox.warning(self, "警告", "请输入符合条件的密码!")
self.input_pass1.setText('')
self.input_pass2.setText('')
# 键盘确认
def keyPressEvent(self, event):
QWidget.keyPressEvent(self, event)
key = event.key()
if key == Qt.Key_Enter:
self.Reset()
def back_to_login(self):
self.hide()
self.back_signal.emit() # 触发返回登陆界面的信号
4、MainWindow.py
import random
from PyQt5.QtCore import pyqtSignal, Qt
from PyQt5.QtGui import QStandardItemModel, QStandardItem, QIcon
from PyQt5.QtWidgets import *
from Main_UI import Ui_MainWindow
from Functions import *
# 在PyQt5里创建的是MainWindow,不是Widget,需要注意的是,创建的元素一定要调用正确。
class MainWindow(QMainWindow, Ui_MainWindow):
insert_signal = pyqtSignal()
batch_signal = pyqtSignal()
query_flag =