今天学习了PyQt5的水平排列与垂直排列,学完就tm明年了
from PyQt5.QtCore import Qt from PyQt5.QtGui import QFont from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout, \ QMessageBox, QGridLayout class Login(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Login") self.setFixedSize(1000, 700) # 用户名输入框 self.username_label = QLabel("账号:") self.username_edit = QLineEdit() self.username_layout = QHBoxLayout() self.username_layout.addWidget(self.username_label) self.username_layout.addWidget(self.username_edit) # 设置用户名输入框字体 username_font = QFont() username_font.setPointSize(20) self.username_label.setFont(username_font) # 密码输入框 self.password_label = QLabel("密码:") 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) # 设置密码输入框字体 password_font = QFont() password_font.setPointSize(20) self.password_label.setFont(password_font) # 登录按钮 self.login_button = QPushButton("登录") self.login_button.clicked.connect(self.login) self.login_button.setFixedSize(150, 60) # 注册按钮 self.signup_button = QPushButton("注册") self.signup_button.clicked.connect(self.show_signup) self.signup_button.setFixedSize(150, 60) # 创建水平布局 self.buttons_layout = QHBoxLayout() # 在水平布局中添加两个按钮 self.buttons_layout.addWidget(self.login_button) self.buttons_layout.addStretch(1) self.buttons_layout.addWidget(self.signup_button) # 页面布局 self.layout = QVBoxLayout() self.layout.addLayout(self.username_layout) self.layout.addLayout(self.password_layout) self.layout.addLayout(self.buttons_layout) # 设置当前窗口的布局 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.13,layout,edit,self,今日,password,button From: https://www.cnblogs.com/yangkaiwen/p/17316960.html