今天学习了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_()
标签:总结,username,4.12,layout,edit,self,addWidget,今日,password From: https://www.cnblogs.com/yangkaiwen/p/17311161.html