0

I am writing a program (PyQt5) helping my coworker to process data. I have a button call "newSession", which supposedly should start a new instance of the program (main window) and operates independently.

The problem I want to avoid: in case one main window crashes, it will crash all the existing main windows. (which it does right now)

Here is simplified version, with only two buttons:

import sys
from PyQt5 import QtWidgets, QtCore

instanceList = []

class newWindow(QtWidgets.QWidget):
    def __init__(self) -> None:
        super().__init__()

        self.idx = 0
        self.setTitle()
        self.setGeometry(400, 100, 500, 80)

        btn1 = QtWidgets.QPushButton(self, text='New session')
        btn2 = QtWidgets.QPushButton(self, text='Crash it!')

        LO = QtWidgets.QVBoxLayout(self)
        LO.addWidget(btn1)
        LO.addWidget(btn2)

        self.setLayout(LO)

        btn1.clicked.connect(self.addWindow)
        btn2.clicked.connect(self.crashIt)

    def setTitle(self):
        self.setWindowTitle('Window {0}'.format(self.idx))

    def addWindow(self):
        w = newWindow()
        w.move(self.pos() + QtCore.QPoint(60, 60))
        w.idx = self.idx + 1
        w.setTitle()
        instanceList.append(w)
        w.show()

    def crashIt(self):
        a.b = 40

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = newWindow()
    instanceList.append(window)
    window.show()
    sys.exit(app.exec_())
ym3141
  • 66
  • 4
  • 2
    Windows are not processes. Instantiating `newWindow()` doesn't start a new instance of the program, it just creates a new window. – Jussi Nurminen Feb 22 '22 at 12:51
  • 1
    Also, a window cannot "crash". And a program doesn't normally crash randomly: if it does, it probably is due to a bug in your code, so you have to fix it. – musicamante Feb 22 '22 at 13:05
  • The so-called "crash" here is just an unhandled exception. A simple solution would be to use a centralised exception-handler for all the windows. This can be achieved by using an [exception-hook](https://docs.python.org/3/library/sys.html#sys.excepthook) (see also [here](https://stackoverflow.com/a/45800282/984421) and [here](https://stackoverflow.com/a/33741755/984421)). – ekhumoro Feb 22 '22 at 19:18
  • Thanks. For sure in this example, this is just an uncaught exception, but the full version will be way more complex and I just don't want user to lose all data for bug triggered in a single window. I will try to exception-hook method. – ym3141 Feb 22 '22 at 21:01
  • 1
    Have you tried the `subprocess` module to attach a new process to the main window? You could probably use threading as well? – analytical_prat May 16 '22 at 12:43
  • @analytical_prat, Yes. Right now I use a multiprocessing.Process to start a completely new QApplication instance. It works as intended in python env, but have some weird bug (not passaging arguments), when packed with PyInstaller... Will need to deal with that later. – ym3141 May 16 '22 at 20:28

0 Answers0