非常抱歉,下面是经过修正的代码:
import sys
from PySide6.QtCore import Qt, Signal, QTimer
from PySide6.QtGui import QPalette, QColor, QFont
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QLabel, QLineEdit, QPushButton, QWidget, \
QMessageBox, QProgressBar
class 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())
希望这样能够满足您的需求。如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |