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