Open PyQt5 window in child multiprocessing.Process (Python3)

1.3k views Asked by At

I'm trying to open a second window in a new process to not freeze the main window with PyQt5. For this reason, I define a new class that inherits from multiprocessing.Process and shows the window. This is the main code:

class GuiMain(QMainWindow):
    ...
    # Main window with several functions. When a button is clicked, executes 
    # self.button_pressed()

    def button_pressed(self):
        proc1 = OpenWindowProcess()
        proc1.start()


class OpenWindowProcess(mp.Process)

    def __init__(self):
        mp.Process.__init__(self)
        print(self.pid)

    def run(self):
        print("Opening window...")
        window = QtGui.QWindow()
        window.show()
        time.sleep(10)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    application = GuiMain()
    sys.exit(app.exec_())

The process is created and gets a PID. When run() function is called, the message "Opening window.." is displayed, but nothing else happens. No window, no error... I can't figure out whats happening. Thank you in advance!

1

There are 1 answers

0
edusan1213 On

I've come to a solution. You have to create a new QtApplication and attach to it a new QMainWindow instance. This code works fine:

class GuiMain(QMainWindow):
    ...
    # Main window with several functions. When a button is clicked, executes 
    # self.button_pressed()

    def button_pressed(self):
        proc1 = OpenWindowProcess()
        proc1.start()


class OpenWindowProcess(mp.Process)

    def __init__(self):
        mp.Process.__init__(self)
        print("Process PID: " + self.pid)

    def run(self):
        print("Opening window...")
        app = QApplication(sys.argv)
        window = QMainWindow()
        window.show()
        sys.exit(app.exec_())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    application = GuiMain()
    sys.exit(app.exec_())