一、代码实现
import sysfrom PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QLineEdit, QMessageBox
from PyQt6.uic import loadUi
from PyQt6.QtCore import Qtclass 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, QVBoxLayoutself.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:
Hello.ui: