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


 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):
            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.fields = fields_adjustments
            self.qle01 = LineEdit(doubleClicked=self.on_doubleClicked)
            self.qle02 = LineEdit(doubleClicked=self.on_doubleClicked)
            lay = QtGui.QVBoxLayout(self)

            self.btn01_nu_qlinedit_field = QtGui.QPushButton('Close')
            self.btn01_nu_qlinedit_field.setToolTip("Close Form")

            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")

            if self.fields=="closeWin_and_reinit":
                self.btn03 = QtGui.QPushButton('Button No. 3')
                self.btn03.setToolTip("Button No. 3")

        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')

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

        def fnQuit_button_clicked(self, event=None, button_clicked=True):
            print('Clicked: fnQuit_button_clicked')

    def main(fields_adjustments='Nothing_new'):
        app01 = QtGui.QApplication(sys.argv)
        w = MainWindow(fields_adjustments)
        # 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

    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__":
Jul 10, 2019 in Python by marceepoo
• 130 points

edited Jul 22, 2019 by Kalgi 2,681 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
• 57,550 points
0 votes
4 answers

What is a Tuple in Python and how to use it?

Tuples  are a  Unchanging sequence of values, ...READ MORE

answered Jun 21 in Python by sahil
• 540 points
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, 2019 in Python by Taj
• 1,060 points
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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,170 views
0 votes
0 answers
+4 votes
6 answers