效果图
Login.py(登录窗口)文件
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
#import qdarkstyle
import base64
import configparser
from qq import Main
from admin1 import Ui_Form
import hashlib
class logindialog(QDialog,Ui_Form):
is_admin_signal = pyqtSignal()
is_student_signal = pyqtSignal(str)
def __init__(self, *args, **kwargs):
# super().__init__(*args, **kwargs)
super(logindialog, self).__init__(*args, **kwargs)
self.setupUi(self) ##初始化界面内容
self.setFixedSize(self.width(), self.height()) #禁止窗口大小拉伸
# ###### 设置界面控件
self.setUpUI()
self.load_config()
def setUpUI(self):
self.pushButton.clicked.connect(self.signInCheck)
self.lineEdit_2.returnPressed.connect(self.signInCheck)
self.lineEdit.returnPressed.connect(self.signInCheck)
def signInCheck(self):
studentId = self.lineEdit.text()
password = self.lineEdit_2.text()
#print('studentId',studentId, password)
if (studentId == "" or password == ""):
print(QMessageBox.warning(self, "警告", "账户和密码不可为空!", QMessageBox.Yes, QMessageBox.Yes))
return
# 打开数据库连接
else:
# th=tesql()
#result = th.select_tb_admin(studentId)#加载数据库获取密码
result=(7, 5, 'admin', 'e10adc3949ba59abbe56e057f20f883e')# 本地直接写死 密码采用md5加密 : 123456
hl = hashlib.md5()
hl.update(password.encode(encoding='utf-8'))
#result
if result[2]==0:
print(QMessageBox.information(self, "提示", "该账号不存在!", QMessageBox.Yes, QMessageBox.Yes))
return
else:
if (5==result[1] and hl.hexdigest() == result[3]):
self.is_student_signal.emit(studentId)
elif (5 != result[1] and hl.hexdigest() == result[3]):
print(QMessageBox.information(self, "提示", "账户类型错误!", QMessageBox.Yes))
return
else:
print(QMessageBox.information(self, "提示", "密码错误!", QMessageBox.Yes))
return
# 通过验证,关闭对话框并返回1
self.login()
self.accept()
def load_config(self):
config = configparser.ConfigParser()
file = config.read('user.ini')
config_dict = config.defaults()
self.user_name = config_dict['user_name']
self.lineEdit.setText(self.user_name)
if config_dict['remember'] == 'True':
self.password = config_dict['password']
print('self.password',self.password)
plaintext = str(self.password).split('-')[1]
print('plaintext__s1',plaintext)
s1 = base64.b64decode(plaintext).decode()
print('plaintext__s1', s1)
#'BDUSS=MwczNOVjNMSjdqcmlJNDQwZFVyUzIwTnl4TE0xZFA1eWZ-'+s+'-RTFtUlFQOGJkaTlnSVFBQUFBJ'
self.lineEdit_2.setText(s1)
self.checkBox.setChecked(True)
else:
self.checkBox.setChecked(False)
def login(self):
self.user_name = self.lineEdit.text()
self.password = self.lineEdit_2.text()
plaintext = self.password
s = base64.b64encode(plaintext.encode('utf-8')).decode()
print('plaintext', plaintext)
pwd = 'BDUSS=MwczNOVjNMSjdqcmlJNDQwZFVyUzIwTnl4TE0xZFA1eWZ-'+str(s)+'-RTFtUlFQOGJkaTlnSVFBQUFBJBDRCVFR=[Fc9oatPmwxn]=srT4swvGNE6uzdhUL68mv3'
config = configparser.ConfigParser()
if self.checkBox.isChecked():
config["DEFAULT"] = {
"user_name": self.user_name,
"password": pwd,
"remember": self.checkBox.isChecked()
}
else:
config["DEFAULT"] = {
"user_name": self.user_name,
"password": "",
"remember": self.checkBox.isChecked()
}
with open('user.ini', 'w')as configfile:
config.write((configfile))
print(self.user_name, self.password)
################################################
#######程序入门
################################################
if __name__ == "__main__":
app = QApplication(sys.argv)
#app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
dialog = logindialog()
if dialog.exec_()==QDialog.Accepted:
window = Main()
window.show()
sys.exit(app.exec_())
admin1.py #UI文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'admin.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5.QtGui import QPixmap, QPainter, QColor, QFont, QIcon
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QApplication, QLabel, QDesktopWidget, QHBoxLayout, QFormLayout, \
QPushButton, QLineEdit
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(504, 314)
# self.label = QtWidgets.QLabel(Form)
# self.label.setGeometry(QtCore.QRect(0, 10, 500, 301))
# self.label.setMouseTracking(False)
# self.label.setText("")
# self.label.setScaledContents(False)
# self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(145, 130, 201, 41)) #uers
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(Form)
self.lineEdit_2.setGeometry(QtCore.QRect(145, 188, 201, 41)) #pwd
self.lineEdit_2.setMaxLength(16)
passwordFont = QFont()
passwordFont.setPixelSize(16)
self.lineEdit_2.setFont(passwordFont)
self.lineEdit_2.setEchoMode(QLineEdit.Password)
self.lineEdit_2.setObjectName("lineEdit_2")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(175, 252, 141, 41)) #login
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(60, 134, 71, 31))
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(13)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2") #user
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(62, 197, 71, 31))
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(13)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3") #pwd
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(0, 0, 504, 120))
# 添加顶部logo图片
pixmap = QPixmap("resources/icons/10.jpg")
scaredPixmap = pixmap.scaled(650, 140)
self.label_4.setPixmap(scaredPixmap)
self.checkBox = QtWidgets.QCheckBox(Form) #记住密码
self.checkBox.setGeometry(QtCore.QRect(360, 197, 90, 27))
self.checkBox.setObjectName("checkBox")
font.setPointSize(10)
self.checkBox.setFont(font)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "智能平台"))
self.pushButton.setText(_translate("Form", "登 录"))
self.label_2.setText(_translate("Form", "用户名:"))
self.label_3.setText(_translate("Form", "密 码:"))
self.pushButton.setFont(QFont("Microsoft YaHei"))
self.pushButton.setObjectName("login_btn")
self.pushButton.setStyleSheet("#login_btn{background-color:#2c7adf;color:#fff;border:none;border-radius:4px;}")
self.checkBox.setText(_translate("Form", "记住密码"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
form = QtWidgets.QWidget()
w = Ui_Form()
w.setupUi(form)
form.show()
sys.exit(app.exec_())
#跳转主页面:
qq.py
from PyQt5.QtWidgets import *
import sys
class Main(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("主窗口")
button = QPushButton("弹出子窗", self)
button.clicked.connect(self.show_child)
def show_child(self):
self.child_window = Child()
self.child_window.show()
class Child(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("我是子窗口啊")
# 运行主窗口
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
#跳转效果图