当然可以,我们可以按照您的需求来逐步实现。首先,建立一个BaseWindow,设置窗口大小为1440x900,并将背景色设置为淡蓝色。添加ESC事件和退出按钮,并在点击退出按钮时关闭应用程序。代码如下:
import sys
from PySide6.QtCore import Qt
from PySide6.QtGui import QKeyEvent, QColor
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QMessageBox
class BaseWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Base Window")
self.setGeometry(100, 100, 1440, 900)
self.setStyleSheet("background-color: lightblue;")
def keyPressEvent(self, event: QKeyEvent):
if event.key() == Qt.Key_Escape:
self.close()
def closeEvent(self, event):
reply = QMessageBox.question(self, "Exit", "Are you sure you want to exit?", QMessageBox.Yes | QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == "__main__":
app = QApplication(sys.argv)
base_window = BaseWindow()
base_window.show()
sys.exit(app.exec())
接下来,我们要创建欢迎界面和用户登录界面两个窗口。我们可以使用PySide6的QPushButton作为按钮,以及QHBoxLayout和QVBoxLayout来进行布局。当用户名和密码正确时,可以根据需要显示5个按钮。
下面是一个示例代码,演示了如何添加欢迎界面和用户登录界面,并在登录成功后显示5个按钮。请注意,示例代码中的登录验证仅作为示例,您需要将其替换为适合您实际情况的验证逻辑。
import sys
from PySide6.QtCore import Qt
from PySide6.QtGui import QKeyEvent
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QLineEdit, QMessageBox
class WelcomeWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Welcome")
self.setGeometry(100, 100, 1440, 900)
self.setStyleSheet("background-color: lightblue;")
layout = QVBoxLayout()
label = QLabel("Welcome to the App!", self)
layout.addWidget(label)
login_button = QPushButton("Login", self)
login_button.clicked.connect(self.show_login_window)
layout.addWidget(login_button)
self.central_widget = QWidget(self)
self.central_widget.setLayout(layout)
self.setCentralWidget(self.central_widget)
def show_login_window(self):
login_window = LoginWindow(self)
login_window.login_success.connect(self.show_main_window)
self.setCentralWidget(login_window)
class LoginWindow(QMainWindow):
login_success = Signal()
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Login")
self.setGeometry(100, 100, 1440, 900)
self.setStyleSheet("background-color: lightblue;")
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)
layout.addWidget(self.input_password)
login_button = QPushButton("Login", self)
login_button.clicked.connect(self.login)
layout.addWidget(login_button)
self.central_widget = QWidget(self)
self.central_widget.setLayout(layout)
self.setCentralWidget(self.central_widget)
def keyPressEvent(self, event: QKeyEvent):
if event.key() == Qt.Key_Escape:
self.close()
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, "Login Failed", "Invalid username or password!")
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Main Window")
self.setGeometry(100, 100, 1440, 900)
self.setStyleSheet("background-color: lightblue;")
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)
self.central_widget = QWidget(self)
self.central_widget.setLayout(layout)
self.setCentralWidget(self.central_widget)
if __name__ == "__main__":
app = QApplication(sys.argv)
welcome_window = WelcomeWindow()
login_window = LoginWindow()
main_window = MainWindow()
welcome_window.show()
login_window.login_success.connect(welcome_window.show_main_window)
welcome_window.main_window_request.connect(main_window.show)
sys.exit(app.exec())
这个示例代码中,我们添加了`WelcomeWindow`类和`MainWindow`类来分别表示欢迎界面和主窗口。`LoginWindow`类则用于处理用户的登录。在登录成功后,通过信号槽机制将登录成功的信号传递给欢迎界面,然后显示主窗口。
请注意,以上代码仅提供了基本的界面示例,您可以根据自己的需求进行进一步开发和扩展。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |