import sys
from PyQt5.Qt import *
class Window_2(QWidget):
def closeEvent(self, a0) -> None:
self.slot_func()
return super().closeEvent(a0)
def close_signal(self, slot_func):
self.slot_func = slot_func
class Window(QWidget):
def __init__(self):
super().__init__()
self.setup_ui()
self.open_window_2()
def setup_ui(self):
self.setWindowTitle("窗口1")
self.resize(300, 300)
self.label = QLabel(self)
self.label.resize(200, 30)
self.label.setStyleSheet("background-color:cyan")
self.show()
def open_window_2(self):
self.window_2 = Window_2()
self.window_2.setWindowTitle("窗口2")
self.window_2.resize(200, 100)
self.window_2_push_button = QPushButton(self.window_2)
self.window_2_push_button.setText("按钮")
def window_2_push_button_clicked():
self.label.setText("窗口2按钮被点击了")
self.window_2_push_button.clicked.connect(window_2_push_button_clicked)
def window_2_closed():
self.label.setText("窗口2被关闭了")
self.window_2.close_signal(window_2_closed)
self.window_2.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())