2

I have created a gui in PyQt5, and have several window-classes controlled by a class called Controller.

Background: In one of the window(A) you can push buttons and a new window(B) pops up and A is disabled. Then when quit button is pressed from window B it goes back to window A without saving edits in window B and enables window A, and if continue button is pressed in B, then it reopens window A and enables it.

Problem: However, when the x button in the corner of window B is pressed the window is closed and window A is disabled. My question is therefore: How can I make window A enable again when B is closed? Can I override the red corner x event such that I can be able to enable window A again?

PS! I tried to make the code minimal and reproducible, let me know if there is something I should fix:)

import sys
from PyQt5 import QtCore, QtWidgets

class WindowTwo(QtWidgets.QWidget):
    switch_window = QtCore.pyqtSignal()
    def __init__(self, M, S):
        QtWidgets.QWidget.__init__(self)
        self.S              = S
        self.M              = M
        self.setupUi                         (   self )
        self.setWindowTitle                  (  'M'   )
    def setupUi(self, CM):
        CM.setEnabled             (   True    )
        CM.setFocusPolicy         (   QtCore.Qt.TabFocus   )
        layout = QtWidgets.QGridLayout()
        layout.addWidget(QtWidgets.QLabel("M: " ),0,0)
        layout.addWidget(QtWidgets.QLabel("S: " ),0,1)
        self.QuitButton      = QtWidgets.QPushButton ( "Quit"     )
        self.QContinueButton = QtWidgets.QPushButton ( "Continue"      )
        self.QuitButton.clicked.connect  ( CM.close        )
        self.QContinueButton.clicked.connect( lambda: self.windowtwo(self.M))
        layout.addWidget( self.QuitButton, 10, 1    )
        layout.addWidget ( self.QContinueButton, 10, 2         )

        self.setLayout   ( layout )
    def windowtwo( self, M):
        self.M = M
        self.switch_window.emit()

class MS(QtWidgets.QWidget):
    switch_window = QtCore.pyqtSignal(int)
    def __init__(self, M, S):
        QtWidgets.QWidget.__init__(self)
        self.S              = S
        self.M              = M
        self.setupUi                         (   self )
        self.setWindowTitle                  (  'M'   )
    def setupUi(self, CM):
        CM.setEnabled             (   True    )
        CM.setFocusPolicy         (   QtCore.Qt.TabFocus   )
        layout = QtWidgets.QGridLayout()
        layout.addWidget(QtWidgets.QLabel("M: " ),0,0)
        layout.addWidget(QtWidgets.QLabel("S: " ),0,1)
        self.QWindowTwoButton = QtWidgets.QPushButton ( ' Go to other window')
        self.QuitButton      = QtWidgets.QPushButton ( "Quit"     )
        self.QContinueButton = QtWidgets.QPushButton ( "Continue"      )
        self.QuitButton.clicked.connect  ( CM.close        )
        self.QWindowTwoButton.clicked.connect( lambda b=0, a= 400 : self.windowms(a))
        layout.addWidget( self.QuitButton, 10, 1    )
        layout.addWidget ( self.QContinueButton, 10, 2         )
        layout.addWidget ( self.QWindowTwoButton, 10, 3         )

        self.setLayout   ( layout )
    def windowms( self, a):
        a = 100
        self.switch_window.emit(a)


class Controller:

    def __init__(self):
            self.M                    = 5
            self.S                    = 7
            self.A                    = 8
            # pass


    def show_window_two(self):
        self.window_two = WindowTwo(self.M, self.S)
        self.window_two.switch_window.connect(self.show_window_three)
        self.window_two.show()


    def show_window_three(self):
        try:
            self.A = self.window_four.A
        except:
            pass
        self.window_three = MS(self.M, self.S)
        self.mscrollArea = QtWidgets.QScrollArea()
        self.mscrollArea.setWidget(self.window_three)
        self.mscrollArea.setDisabled(0)
        self.window_three.switch_window.connect(self.show_window_four)
        self.window_three.QuitButton.clicked.connect(self.mscrollArea.close)

        self.mscrollArea.show()
        self.mscrollArea.resize(700, 500)
        self.mscrollArea.setWindowTitle("MS")

        self.window_two.close()
        try: 
            self.window_four.close()
        except:
            pass

    def show_window_four(self, a):
        if (a == 100):
            self.window_four = WindowTwo(self.M, self.S)
            self.window_four.switch_window.connect(self.show_window_three)
            self.mscrollArea.setDisabled(1)
            self.window_four.QuitButton.clicked.connect(lambda: self.window_four.close)
            self.window_four.QuitButton.clicked.connect(lambda: self.mscrollArea.setDisabled(0))
            self.window_four.show()
       #Here is an else if a is other values to open other windows

def main():
    app = QtWidgets.QApplication(sys.argv)
    controller = Controller()
    controller.show_window_two()
    sys.exit(app.exec_())



if __name__ == '__main__':
    main()
AuroraS
  • 59
  • 8

1 Answers1

2

The closeEvent is fired whenever a widget is closed, for example by clicking the "X" in the corner. Just override it with your own code to show the main window again, or emit a signal to inform your Controller.

from PyQt5 import QtWidgets
app = QtWidgets.QApplication.instance() or QtWidgets.QApplication([])

class W1(QtWidgets.QWidget):    
    def closeEvent(self, event):
        print("closing w1")

class W2(QtWidgets.QWidget):    
    def __init__(self, w1):
        super().__init__()
        self.w1 = w1        

    def closeEvent(self, event):
        print("closing w2")
        self.w1.show()

w1 = W1()
w2 = W2(w1)

w2.show()
app.exec_()
Jeronimo
  • 2,268
  • 2
  • 13
  • 28