|
楼主 |
发表于 2024-4-8 10:53:07
|
显示全部楼层
请问这一段代码:
import sys
from PySide6.QtCore import Qt, Signal
from PySide6.QtGui import QPalette, QColor, QFont
from PySide6.QtWidgets import (QApplication, QMainWindow, QVBoxLayout, QLabel,
QLineEdit, QPushButton, QWidget, QMessageBox)
class BaseWindow(QMainWindow):
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)
self.setup_ui()
def setup_ui(self):
main_widget = QWidget(self)
layout = QVBoxLayout(main_widget)
label = QLabel("Username:", main_widget)
layout.addWidget(label)
line_edit = QLineEdit(main_widget)
layout.addWidget(line_edit)
button_login = QPushButton("Login", main_widget)
layout.addWidget(button_login)
button_exit = QPushButton("Exit", main_widget)
layout.addWidget(button_exit)
button_exit.clicked.connect(self.confirm_exit)
self.setCentralWidget(main_widget)
def confirm_exit(self):
reply = QMessageBox.question(self, "确认", "确定要退出系统吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
self.close()
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 == "1" and password == "1":
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())
button_exit为什么会点不动呢? |
|