import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QMessageBox
from PyQt5.QtCore import QObject, QEvent
class HomePage(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Home Page")
self.setGeometry(100, 100, 300, 200)
def closeEvent(self, event):
# 在这里添加你想执行的关闭操作
print("Closing Home Page")
def eventFilter(self, obj, event):
if event.type() == QEvent.Close:
# 弹出确认关闭的弹窗
reply = QMessageBox.question(self, 'Message', 'Are you sure you want to close?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
return super().eventFilter(obj, event)
class LoginPage(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Login Page")
self.setGeometry(100, 100, 200, 100)
self.login_button = QPushButton("Login", self)
self.login_button.setGeometry(50, 30, 100, 30)
self.login_button.clicked.connect(self.login)
def login(self):
self.home_page = HomePage()
self.home_page.installEventFilter(self.home_page) # 安装事件过滤器
self.home_page.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
login_page = LoginPage()
login_page.show()
sys.exit(app.exec_())