How to close a window, and then reinitialize it using PyQt

+1 vote

There is a button "btn_closeWin_and_reinit"  in the PyQt4 window "MainWindow" produced by the python script below..

I want that button to close the MainWindow, and then re-start the MainWindow with the argument

        fields_adjustments="three_btns"

 so that the re-initialized window will have all three of the buttons I coded.

When I click on the button "btn_closeWin_and_reinit", my script below produces crashes the MainWindow and produces the error message, "QCoreApplication::exec: The event loop is already running"

I would appreciate help in figuring out how to re-start the MainWindow so that the argument fields_adjustments="three_btns" is effective in adding btn03 to the re-initialized MainWindow.

    # coding: UTF-8
    """

    """

    import sys

    from PyQt4 import QtCore, QtGui

    class LineEdit(QtGui.QLineEdit):
        doubleClicked = QtCore.pyqtSignal()

        def mouseDoubleClickEvent(self, event):
            self.doubleClicked.emit()
            super(LineEdit, self).mouseDoubleClickEvent(event)

    class MainWindow(QtGui.QWidget):
        def __init__(self, fields_adjustments='Nothing_new', *args, **kwargs):
            super(MainWindow, self).__init__(*args, **kwargs)
            self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
            self.fields = fields_adjustments
            self.qle01 = LineEdit(doubleClicked=self.on_doubleClicked)
            self.qle02 = LineEdit(doubleClicked=self.on_doubleClicked)
            lay = QtGui.QVBoxLayout(self)
            lay.addWidget(self.qle01)
            lay.addWidget(self.qle02)

            self.btn01_nu_qlinedit_field = QtGui.QPushButton('Close')
            self.btn01_nu_qlinedit_field.setToolTip("Close Form")
            self.btn01_nu_qlinedit_field.clicked.connect(self.fnQuit_button_clicked)
            self.btn01_nu_qlinedit_field.resize(self.btn01_nu_qlinedit_field.sizeHint())
            lay.addWidget(self.btn01_nu_qlinedit_field)

            self.btn_closeWin_and_reinit = QtGui.QPushButton()
            self.btn_closeWin_and_reinit.setText('Close and Reopen Win with 3 Btns')
            self.btn_closeWin_and_reinit.setToolTip("Close & Re-Initialize w/3 qlineedits")
            self.btn_closeWin_and_reinit.clicked.connect(self.fnCallOutsideFn)
            self.btn_closeWin_and_reinit.resize(self.btn_closeWin_and_reinit.sizeHint())
            lay.addWidget(self.btn_closeWin_and_reinit)

            if self.fields=="closeWin_and_reinit":
                self.btn03 = QtGui.QPushButton('Button No. 3')
                self.btn03.setToolTip("Button No. 3")
                self.btn03.clicked.connect(self.fnQqch)
                self.btn03.resize(self.btn03.sizeHint())
                lay.addWidget(self.btn03)

        @QtCore.pyqtSlot()
        def on_doubleClicked(self):
            if self.sender() is self.qle01:
                print("The QLineEdit field's name is 'qle01'.")
            elif self.sender() is self.qle02:
                print("The QLineEdit field's name is 'qle02'.")

        def fnCallOutsideFn(self):
            print('Line 60')
            fn_closeWin_and_reinit_w_3btns(self)

        def fnQqch(self):
            print('Clicked: fnQqch_button_clicked')

        def fnQuit_button_clicked(self, event=None, button_clicked=True):
            print('Clicked: fnQuit_button_clicked')
            QtCore.QCoreApplication.instance().quit()

    def main(fields_adjustments='Nothing_new'):
        app01 = QtGui.QApplication(sys.argv)
        w = MainWindow(fields_adjustments)
        w.show()
        # https://stackoverflow.com/questions/12118939/how-to-make-a-pyqt4-window-jump-to-the-front
        # this will remove minimized status
        # and restore window with keeping maximized/normal state
        w.setWindowState(w.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive)
        # this will activate the window
        w.activateWindow()
        sys.exit(app01.exec_())

    def fn_closeWin_and_reinit_w_3btns(window_object):#(instans):
        print('line 82')
    #    QtCore.QCoreApplication.instance().quit() # this line does NOT help me avoid the error message: QCoreApplication::exec: The event loop is already running
    #    window_object.close() # this line does NOT help me avoid the error message: QCoreApplication::exec: The event loop is already running
        print('Line 85')
        main(fields_adjustments="three_btns") # this line produces the error message: QCoreApplication::exec: The event loop is already running

    if __name__ == "__main__":
        main(fields_adjustments='Nothing_new')
Jul 10 in Python by marceepoo
• 130 points

edited Jul 22 by Kalgi 117 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

How to open another window using PyQt

Switch between self.hide() and self.showMinimized() def start(self): ...READ MORE

answered Oct 1, 2018 in Python by Priyaj
• 56,940 points
3,153 views
0 votes
3 answers
0 votes
1 answer
0 votes
1 answer

How to create a train and test sample from one dataframe using pandas?

Hi, The below written code can help you ...READ MORE

answered Jul 3 in Python by Taj
• 1,040 points
305 views
0 votes
1 answer
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 349 views
+4 votes
6 answers