|
发表于 2024-4-8 17:53:28
|
显示全部楼层
非常抱歉给您带来困惑。在上面的代码中,进度条一闪而过是因为主窗口在显示进度条的同时立即关闭了,所以无法看到进度条效果。为了能够正常显示进度条,可以进行如下修改:import sysfrom PySide6.QtCore import Qt, Signal, QTimerfrom PySide6.QtGui import QPalette, QColor, QFontfrom PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QLabel, QLineEdit, QPushButton, QWidget, \ QMessageBox, QProgressBarclass BaseWindow(QMainWindow): switch_to_a = Signal() # 切换到A窗口信号 login_success = Signal() # 登录成功信号 def __init__(self): super().__init__() self.setWindowTitle("Base Window") self.setGeometry(100, 100, 800, 600) palette = self.palette() palette.setColor(QPalette.Window, QColor("#87CEFA")) self.setPalette(palette) layout = QVBoxLayout() label = QLabel("Welcome to the App!", self) label.setFont(QFont("Arial", 24, QFont.Bold)) layout.addWidget(label) label_username = QLabel("Username:", self) layout.addWidget(label_username) self.input_username = QLineEdit(self) layout.addWidget(self.input_username) label_password = QLabel("Password:", self) layout.addWidget(label_password) self.input_password = QLineEdit(self) self.input_password.setEchoMode(QLineEdit.Password) layout.addWidget(self.input_password) button_login = QPushButton("Login", self) button_login.clicked.connect(self.login) layout.addWidget(button_login) button_quit = QPushButton("Quit", self) button_quit.clicked.connect(self.confirm_quit) layout.addWidget(button_quit) self.progress_bar = QProgressBar(self) self.progress_bar.setTextVisible(False) self.progress_bar.setRange(0, 100) self.progress_bar.setValue(0) self.progress_bar.setVisible(False) layout.addWidget(self.progress_bar) central_widget = QWidget(self) central_widget.setLayout(layout) self.setCentralWidget(central_widget) self.try_count = 0 def keyPressEvent(self, event): if event.key() == Qt.Key_Escape: self.confirm_quit() def confirm_quit(self): reply = QMessageBox.question( self, "Confirmation", "Are you sure you want to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No ) if reply == QMessageBox.Yes: self.close() def login(self): username = self.input_username.text() password = self.input_password.text() if username == "1" and password == "1": self.login_success.emit() else: QMessageBox.warning( self, "Login Failed", "Invalid username or password. Please try again." ) self.input_username.clear() self.input_password.clear() self.input_username.setFocus() self.try_count += 1 if self.try_count >= 3: print("Login Failed333")class LoginWindow(BaseWindow): def __init__(self): super().__init__() self.setWindowTitle("Login Window") self.login_success.connect(self.show_progress_bar) def show_progress_bar(self): self.progress_bar.setVisible(True) self.timer = QTimer() self.timer.timeout.connect(self.update_progress_bar) self.timer.start(100) # 每100ms更新一次进度条 def update_progress_bar(self): value = self.progress_bar.value() + 10 self.progress_bar.setValue(value) if value >= 100: self.timer.stop() self.switch_to_a.emit() self.timer.deleteLater() self.progress_bar.setVisible(False) self.progress_bar.setValue(0)class WindowA(BaseWindow): def __init__(self): super().__init__() self.setWindowTitle("Window A") self.setGeometry(100, 100, 800, 600) palette = self.palette() palette.setColor(QPalette.Window, QColor("#98FB98")) self.setPalette(palette) layout = QVBoxLayout() label = QLabel("This is Window A", self) label.setFont(QFont("Arial", 24, QFont.Bold)) layout.addWidget(label) central_widget = QWidget(self) central_widget.setLayout(layout) self.setCentralWidget(central_widget)if __name__ == "__main__": app = QApplication(sys.argv) login_window = LoginWindow() window_a = WindowA() login_window.switch_to_a.connect(window_a.show) login_window.show() sys.exit(app.exec())请注意,我将登录窗口和进度条相关的逻辑分开到一个单独的`LoginWindow`类中,并使用了信号`login_success`来触发显示进度条的操作。这样在登录成功后进度条才会显示,并正常展示进度。之后在进度条达到最大值时,切换到窗口A并隐藏进度条。希望这次能够满足您的需求。如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|