|
发表于 2024-2-17 11:57:13
|
显示全部楼层
非常抱歉,我之前的回答有误。在`show_login_window`方法中,你应该将`login_window`声明为`WelcomeWindow`类的一个属性,并在登录成功后调用`self.login_window.close()`关闭登录窗口。然后,你可以在`open_main_window`方法中创建和显示主窗口。以下是修改后的代码示例:import sysfrom PySide6.QtCore import Qt, QTimerfrom PySide6.QtGui import QFont, QColor, QPalette, QPixmap, QPainter, QPen, QBrush, QLinearGradient, QPainterPathfrom PySide6.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QProgressBar, QLineEdit, QPushButton, QMessageBox, QHBoxLayoutclass WelcomeWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle('欢迎界面') self.setGeometry(300, 100, 1400, 900) # 设置字体 font = QFont('隶书', 40, QFont.Bold) # 创建标签显示欢迎词 self.welcome_label = QLabel(self) self.welcome_label.setText('欢迎') self.welcome_label.setFont(font) self.welcome_label.move(700, 400) self.welcome_label.setFixedSize(200, 50) # 设置背景图片 background_label = QLabel(self) background_image = QPixmap('路径/到/你的/背景图片') background_label.setPixmap(background_image.scaled(self.size())) background_label.resize(self.size()) # 创建进度条 self.progress_bar = QProgressBar(self) self.progress_bar.setGeometry(450, 830, 500, 30) self.progress_bar.setTextVisible(True) self.progress_bar.setStyleSheet(""" QProgressBar::chunk { background-color: #2196F3; width: 10px; } """) # 设置定时器每秒更新进度条 self.timer = QTimer(self) self.timer.timeout.connect(self.update_progress) self.timer.start(50) def update_progress(self): value = self.progress_bar.value() if value < 100: value += 1 self.progress_bar.setValue(value) else: self.timer.stop() self.show_login_window() def show_login_window(self): self.login_window = QWidget() self.login_window.setWindowTitle('用户登录') # 创建用户名和密码输入框 username_label = QLabel('用户名:', self.login_window) username_label.setFont(QFont('隶书', 20)) username_entry = QLineEdit(self.login_window) password_label = QLabel('密码:', self.login_window) password_label.setFont(QFont('隶书', 20)) password_entry = QLineEdit(self.login_window) password_entry.setEchoMode(QLineEdit.Password) # 登录按钮功能 def login(): username = username_entry.text() password = password_entry.text() if username == 'admin' and password == '123456': QMessageBox.information(self.login_window, '提示', '登录成功!') self.login_window.close() self.open_main_window() else: QMessageBox.critical(self.login_window, '错误', '用户名或密码错误!') password_entry.clear() login_button = QPushButton('登录', self.login_window) login_button.setFont(QFont('隶书', 20)) login_button.clicked.connect(login) # 使用垂直布局 layout = QVBoxLayout() layout.addWidget(username_label) layout.addWidget(username_entry) layout.addWidget(password_label) layout.addWidget(password_entry) layout.addWidget(login_button) self.login_window.setLayout(layout) self.login_window.setGeometry(650, 450, 400, 300) self.login_window.show() def open_main_window(self): self.hide() # 隐藏欢迎窗体 main_window = QWidget(None) # 设置父窗口为None main_window.setWindowTitle('主窗体') main_window.setGeometry(100, 100, 600, 600) # 创建按钮 buttons_layout = QVBoxLayout() for i in range(12): button = QPushButton(f'按钮{i+1}') buttons_layout.addWidget(button) main_window.setLayout(buttons_layout) main_window.show()if __name__ == '__main__': app = QApplication(sys.argv) welcome_window = WelcomeWindow() welcome_window.show() sys.exit(app.exec())请确保将代码中的`"路径/到/你的/背景图片"`替换为你实际的背景图片路径。现在,登录成功后主窗口应该能够正确显示和停留在屏幕上。非常抱歉给你带来困惑,希望这次能符合你的要求!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|