pyQt6做一个登陆界面

2024-01-24 16:07:08 浏览数 (2)

程序效果

先上程序运行效果

输入正确的账号密码之后

程序解析

代码语言:python代码运行次数:0复制
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QLabel, QLineEdit, QPushButton, QWidget, QMessageBox

先导入pyqt所需要的库

创造一个对象

代码语言:python代码运行次数:0复制
class LoginWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Login")
        self.setGeometry(100, 100, 400, 300)

        # 创建一个窗口容器
        container = QWidget()
        self.setCentralWidget(container)

        # 创建一个垂直布局
        layout = QVBoxLayout()
        container.setLayout(layout)

        # 创建一个标签和文本框
        self.username_label = QLabel("Username:")
        self.username_edit = QLineEdit()
        layout.addWidget(self.username_label)
        layout.addWidget(self.username_edit)

        # 创建一个标签和文本框
        self.password_label = QLabel("Password:")
        self.password_edit = QLineEdit()
        self.password_edit.setEchoMode(QLineEdit.EchoMode.Password)
        layout.addWidget(self.password_label)
        layout.addWidget(self.password_edit)

        # 创建一个登录按钮
        self.login_button = QPushButton("Login")
        layout.addWidget(self.login_button)

        # 连接按钮的点击事件
        self.login_button.clicked.connect(self.handle_login)

    def handle_login(self):
        username = self.username_edit.text()
        password = self.password_edit.text()

        # 在这里添加验证逻辑
        if username == "A" and password == "123":
            QMessageBox.information(self, "Success", "Login successful!")
        else:
            QMessageBox.critical(self, "Error", "Invalid username or password.")

运行程序:

代码语言:python代码运行次数:0复制
app = QApplication(sys.argv)
window = LoginWindow()
window.show()
sys.exit(app.exec())

完整程序代码

代码语言:python代码运行次数:0复制
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QLabel, QLineEdit, QPushButton, QWidget, QMessageBox


class LoginWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Login")
        self.setGeometry(100, 100, 400, 300)

        # 创建一个窗口容器
        container = QWidget()
        self.setCentralWidget(container)

        # 创建一个垂直布局
        layout = QVBoxLayout()
        container.setLayout(layout)

        # 创建一个标签和文本框
        self.username_label = QLabel("Username:")
        self.username_edit = QLineEdit()
        layout.addWidget(self.username_label)
        layout.addWidget(self.username_edit)

        # 创建一个标签和文本框
        self.password_label = QLabel("Password:")
        self.password_edit = QLineEdit()
        self.password_edit.setEchoMode(QLineEdit.EchoMode.Password)
        layout.addWidget(self.password_label)
        layout.addWidget(self.password_edit)

        # 创建一个登录按钮
        self.login_button = QPushButton("Login")
        layout.addWidget(self.login_button)

        # 连接按钮的点击事件
        self.login_button.clicked.connect(self.handle_login)

    def handle_login(self):
        username = self.username_edit.text()
        password = self.password_edit.text()

        # 在这里添加验证逻辑
        if username == "A" and password == "123":
            QMessageBox.information(self, "Success", "Login successful!")
        else:
            QMessageBox.critical(self, "Error", "Invalid username or password.")


app = QApplication(sys.argv)
window = LoginWindow()
window.show()
sys.exit(app.exec())

0 人点赞