PYQT5登录界面跳转主界面

效果图
在这里插入图片描述
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_())

#跳转效果图在这里插入图片描述

  • 4
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
您可以使用QStackedWidget来实现登录界面界面之间的跳转。具体步骤如下: 1. 在Qt Designer中创建两个窗口,一个是登录界面,一个是界面。 2. 在登录界面中添加一个按钮,当用户点击该按钮时,跳转界面。 3. 在界面中添加一个按钮,当用户点击该按钮时,返回登录界面。 4. 在函数中初始化两个窗口,并使用QStackedWidget将它们添加到堆栈中。 5. 在登录界面中的按钮槽函数中,使用堆栈的setCurrentIndex()函数将堆栈切换到界面。 6. 在界面中的按钮槽函数中,使用堆栈的setCurrentIndex()函数将堆栈切换回登录界面。 以下是一个简单的示例代码: ``` python from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QStackedWidget class LoginWidget(QWidget): def __init__(self): super().__init__() self.login_button = QPushButton("Login") self.login_button.clicked.connect(self.switch_to_main) layout = QVBoxLayout() layout.addWidget(self.login_button) self.setLayout(layout) def switch_to_main(self): stacked_widget.setCurrentIndex(1) class MainWidget(QWidget): def __init__(self): super().__init__() self.back_button = QPushButton("Back") self.back_button.clicked.connect(self.switch_to_login) layout = QVBoxLayout() layout.addWidget(self.back_button) self.setLayout(layout) def switch_to_login(self): stacked_widget.setCurrentIndex(0) if __name__ == '__main__': app = QApplication([]) stacked_widget = QStackedWidget() login_widget = LoginWidget() main_widget = MainWidget() stacked_widget.addWidget(login_widget) stacked_widget.addWidget(main_widget) stacked_widget.show() app.exec_() ``` 在这个示例中,我们创建了两个QWidget窗口:LoginWidget和MainWidget。在LoginWidget中,我们添加了一个QPushButton,当用户点击该按钮时,切换到MainWidget。在MainWidget中,我们添加了一个QPushButton,当用户点击该按钮时,切换回LoginWidget。我们使用QVBoxLayout将按钮添加到QWidget中。 在函数中,我们创建了一个QStackedWidget,并将LoginWidget和MainWidget添加到堆栈中。我们使用setCurrentIndex()函数初始化堆栈并显示它。当用户点击按钮时,我们使用setCurrentIndex()函数切换窗口

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值