目录

PyQt基础简单的窗口化界面搭建以及槽函数跳转

PyQt基础——简单的窗口化界面搭建以及槽函数跳转

一、代码实现

import sys

from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QLineEdit, QMessageBox
from PyQt6.uic import loadUi
from PyQt6.QtCore import Qt


class LoginWindow(QWidget):
    def __init__(self):
        super().__init__()
        # 加载UI文件
        loadUi('QQ登录.ui', self)

        self.label = self.findChild(QLabel,'label')
        self.label.setPixmap(QPixmap("qq.png"))
        self.label.setScaledContents(True)

        self.lineEdit = self.findChild(QLineEdit,'lineEdit')
        self.lineEdit.setPlaceholderText("输入QQ号")

        self.lineEdit_2 = self.findChild(QLineEdit,'lineEdit_2')
        self.lineEdit_2.setPlaceholderText("输入密码")
        self.lineEdit_2.setEchoMode(QLineEdit.EchoMode.Password)
        # 找到登录按钮并连接点击事件
        self.login_button = self.findChild(QPushButton,'pushButton')  # 这里的'loginButton'需要替换为你UI文件中登录按钮的实际对象名
        if self.login_button:
            self.login_button.clicked.connect(self.on_login)

    def on_login(self):
        username = self.lineEdit.text()
        password = self.lineEdit_2.text()
        if username == "admin" and password == "123456":
            self.close()  # 隐藏登录界面
            self.hello_window = HelloWindow()
            self.hello_window.show()
        else:
            QMessageBox.warning(self,"登录失败","用户名或密码错误,请重新输入。")
            self.lineEdit.clear()
            self.lineEdit_2.clear()

class HelloWindow(QWidget):
    def __init__(self):
        super().__init__()
        loadUi('Hello.ui', self)

    def initUI(self):
        # 创建一个简单的 Hello 标签
        from PyQt6.QtWidgets import QLabel, QVBoxLayout
        self.hello_label = QLabel('Hello')

        # 布局管理
        layout = QVBoxLayout()
        layout.addWidget(self.hello_label)

        self.setLayout(layout)
        self.setWindowTitle('Hello')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    login_window = LoginWindow()
    login_window.setWindowFlag(Qt.WindowType.FramelessWindowHint)
    login_window.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
    login_window.show()
    sys.exit(app.exec())

二、ui界面

QQ登录.ui:

https://i-blog.csdnimg.cn/direct/a4e1dd6b790a4390a682a7de0d7d7905.png https://i-blog.csdnimg.cn/direct/84c214c02dfd4f048ae9eba8781cc985.png

Hello.ui:

https://i-blog.csdnimg.cn/direct/85e328fc68ba467886a9fde8fd64e569.png