|
发表于 2024-4-8 10:26:24
|
显示全部楼层
非常抱歉,以下是对代码进行修改的版本,添加了退出按钮并使其能够被后续窗体继承:import sysfrom PySide6.QtCore import Qt, Signalfrom PySide6.QtGui import QPalette, QColor, QFont, QMessageBoxfrom PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QLabel, QLineEdit, QPushButton, QWidgetclass BaseWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Base Window") self.setGeometry(100, 100, 1440, 900) palette = self.palette() palette.setColor(QPalette.Window, QColor("#87CEFA")) # 淡蓝色背景 self.setPalette(palette) self.setup_exit_button() def setup_exit_button(self): button_exit = QPushButton("Exit", self) button_exit.clicked.connect(self.close) button_exit.setStyleSheet("QPushButton {color: red;}") button_exit.move(self.width() - 100, 10) button_exit.setParent(self) def keyPressEvent(self, event): if event.key() == Qt.Key_Escape: # 捕捉Esc键事件 reply = QMessageBox.question(self, "确认", "确定要退出系统吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: self.close()class WelcomeWindow(BaseWindow): switch_to_login = Signal() # 切换到登录界面信号 def __init__(self): super().__init__() layout = QVBoxLayout() label = QLabel("Welcome to the App!", self) label.setFont(QFont("Arial", 24, QFont.Bold)) layout.addWidget(label) button_login = QPushButton("Login", self) button_login.clicked.connect(self.switch_to_login.emit) layout.addWidget(button_login) central_widget = QWidget(self) central_widget.setLayout(layout) self.setCentralWidget(central_widget)class LoginWindow(BaseWindow): login_success = Signal() # 登录成功信号 def __init__(self): super().__init__() layout = QVBoxLayout() 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) central_widget = QWidget(self) central_widget.setLayout(layout) self.setCentralWidget(central_widget) def login(self): username = self.input_username.text() password = self.input_password.text() # 进行用户名和密码的验证逻辑,这里只做简单演示 if username == "admin" and password == "password": self.login_success.emit() else: QMessageBox.warning(self, "登录失败", "用户名或密码错误,请重新输入。") self.input_username.clear() self.input_password.clear()class MainWindow(BaseWindow): def __init__(self): super().__init__() layout = QVBoxLayout() button1 = QPushButton("Button 1", self) layout.addWidget(button1) button2 = QPushButton("Button 2", self) layout.addWidget(button2) button3 = QPushButton("Button 3", self) layout.addWidget(button3) button4 = QPushButton("Button 4", self) layout.addWidget(button4) button5 = QPushButton("Button 5", self) layout.addWidget(button5) central_widget = QWidget(self) central_widget.setLayout(layout) self.setCentralWidget(central_widget)if __name__ == "__main__": app = QApplication(sys.argv) welcome_window = WelcomeWindow() login_window = LoginWindow() main_window = MainWindow() login_window.login_success.connect(main_window.show) welcome_window.switch_to_login.connect(login_window.show) login_window.show() sys.exit(app.exec())现在,BaseWindow中已经添加了一个名为“Exit”的退出按钮,并且在继承该类的其他窗体中也可以看到这个退出按钮。同时,按下Esc键时也会弹出确认对话框。希望这次的修改满足你的需求!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|