|
发表于 2024-4-7 20:02:08
|
显示全部楼层
非常抱歉,之前的代码确实没有完全满足你的需求。在PySide6中,可以通过重写`keyPressEvent`函数来实现继承ESC事件,而退出按钮可以使用`QAction`加入到菜单栏中。以下是修改后的代码示例:import sysfrom PySide6.QtCore import Qt, QTimerfrom PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QProgressBar, QPushButton, QLabel, QActionclass 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())在此代码示例中,我添加了一个`exit_action`来创建退出按钮,并将其添加到菜单栏中。同时,在`WindowA`和`WindowB`中重写了`keyPressEvent`函数,以捕获ESC键事件并关闭对应的窗口。希望这次的修改能够满足你的需求,如还有其他问题,请随时提问!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|