非常抱歉,以下是修改后的代码示例:
import sys
from PySide6.QtCore import Qt, QTimer
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QProgressBar, QPushButton, QLabel, QAction
class BaseWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Base Window")
self.setGeometry(100, 100, 1440, 900)
self.error_count = 0
# 创建布局和控件
layout = QVBoxLayout()
self.progress_bar = QProgressBar()
self.progress_bar.setValue(0)
self.login_button = QPushButton("登录")
# 将进度条和登录按钮添加到布局中
layout.addWidget(self.progress_bar)
layout.addWidget(self.login_button)
# 创建一个主widget,并将布局设置为主widget的布局
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
# 连接信号槽
self.login_button.clicked.connect(self.handle_login)
# 添加退出按钮到菜单栏
exit_action = QAction("退出", self)
exit_action.triggered.connect(self.close)
self.file_menu = self.menuBar().addMenu("文件")
self.file_menu.addAction(exit_action)
def handle_login(self):
# 假设用户名和密码为admin/admin,这里仅作简单的示例
username = "admin"
password = "admin"
if username == "admin" and password == "admin":
self.progress_bar.setValue(0)
self.error_count = 0
# 启动定时器,设置每200毫秒更新进度条的值
self.timer = QTimer()
self.timer.timeout.connect(self.update_progress)
self.timer.start(200)
else:
self.error_count += 1
if self.error_count >= 3:
self.close()
def update_progress(self):
value = self.progress_bar.value() + 10
self.progress_bar.setValue(value)
if value >= 100:
self.timer.stop()
self.close()
self.open_window_a()
def open_window_a(self):
window_a = WindowA()
window_a.show()
class WindowA(BaseWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Window A")
layout = self.centralWidget().layout()
# 在布局最后添加一个提示标签用于测试条件
self.condition_label = QLabel("满足条件")
layout.addWidget(self.condition_label)
self.progress_bar = QProgressBar()
self.progress_bar.setValue(0)
layout.addWidget(self.progress_bar)
self.timer = QTimer()
self.timer.timeout.connect(self.update_progress)
self.timer.start(200)
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.close()
def update_progress(self):
value = self.progress_bar.value() + 10
self.progress_bar.setValue(value)
if value >= 100:
self.timer.stop()
self.close()
self.open_base_window()
def open_base_window(self):
base_window = BaseWindow()
base_window.show()
class WindowB(BaseWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Window B")
layout = self.centralWidget().layout()
self.progress_bar = QProgressBar()
self.progress_bar.setValue(0)
layout.addWidget(self.progress_bar)
self.timer = QTimer()
self.timer.timeout.connect(self.update_progress)
self.timer.start(200)
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.close()
def update_progress(self):
value = self.progress_bar.value() + 10
self.progress_bar.setValue(value)
if value >= 100:
self.timer.stop()
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
base_window = BaseWindow()
base_window.show()
sys.exit(app.exec())
对于代码格式上的问题,再次向您致以诚挚的歉意。希望这次的回复符合您的期望,如果还有其他问题,欢迎随时提问!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |