4.12今日总结

今天学习了Qt的登录注册页面的跳转
复制代码
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout, QMessageBox


class Login(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Login")
        self.setFixedSize(400, 300)

        # 用户名输入框
        self.username_label = QLabel("Username:")
        self.username_edit = QLineEdit()
        self.username_layout = QHBoxLayout()
        self.username_layout.addWidget(self.username_label)
        self.username_layout.addWidget(self.username_edit)

        # 密码输入框
        self.password_label = QLabel("Password:")
        self.password_edit = QLineEdit()
        self.password_edit.setEchoMode(QLineEdit.Password)
        self.password_layout = QHBoxLayout()
        self.password_layout.addWidget(self.password_label)
        self.password_layout.addWidget(self.password_edit)

        # 登录按钮
        self.login_button = QPushButton("Login")
        self.login_button.clicked.connect(self.login)

        # 注册按钮
        self.signup_button = QPushButton("Sign up")
        self.signup_button.clicked.connect(self.show_signup)

        # 页面布局
        self.layout = QVBoxLayout()
        self.layout.addLayout(self.username_layout)
        self.layout.addLayout(self.password_layout)
        self.layout.addWidget(self.login_button)
        self.layout.addWidget(self.signup_button)
        self.setLayout(self.layout)

    def login(self):
        # 在此处写登录逻辑
        username = self.username_edit.text()
        password = self.password_edit.text()

        if username == "admin" and password == "password":
            QMessageBox.information(self, "Login Successful", "Welcome, Admin!")
        else:
            QMessageBox.warning(self, "Login Failed", "Incorrect username or password. Please try again.")

    def show_signup(self):
        self.signup_window = Signup()
        self.signup_window.show()
        self.hide()


class Signup(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Sign up")
        self.setFixedSize(400, 300)

        # 用户名输入框
        self.username_label = QLabel("Username:")
        self.username_edit = QLineEdit()
        self.username_layout = QHBoxLayout()
        self.username_layout.addWidget(self.username_label)
        self.username_layout.addWidget(self.username_edit)

        # 密码输入框
        self.password_label = QLabel("Password:")
        self.password_edit = QLineEdit()
        self.password_edit.setEchoMode(QLineEdit.Password)
        self.password_layout = QHBoxLayout()
        self.password_layout.addWidget(self.password_label)
        self.password_layout.addWidget(self.password_edit)

        # 确认密码输入框
        self.confirm_label = QLabel("Confirm Password:")
        self.confirm_edit = QLineEdit()
        self.confirm_edit.setEchoMode(QLineEdit.Password)
        self.confirm_layout = QHBoxLayout()
        self.confirm_layout.addWidget(self.confirm_label)
        self.confirm_layout.addWidget(self.confirm_edit)

        # 注册按钮
        self.signup_button = QPushButton("Sign up")
        self.signup_button.clicked.connect(self.signup)

        # 返回按钮
        self.back_button = QPushButton("Back to Login")
        self.back_button.clicked.connect(self.show_login)

        # 页面布局
        self.layout = QVBoxLayout()
        self.layout.addLayout(self.username_layout)
        self.layout.addLayout(self.password_layout)
        self.layout.addLayout(self.confirm_layout)
        self.layout.addWidget(self.signup_button)
        self.layout.addWidget(self.back_button)
        self.setLayout(self.layout)

    def signup(self):
        # 在此处写注册逻辑
        username = self.username_edit.text()
        password = self.password_edit.text()
        confirm_password = self.confirm_edit.text()

        if password != confirm_password:
            QMessageBox.warning(self, "Signup Failed", "Password does not match. Please check again.")
        else:
            QMessageBox.information(self, "Signup Successful", f"Welcome, {username}!")
            self.show_login()

    def show_login(self):
        self.login_window = Login()
        self.login_window.show()
        self.hide()


if __name__ == "__main__":
    app = QApplication([])
    login_window = Login()
    login_window.show()
    app.exec_()
复制代码

 

posted @   不洗澡超酷  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示