Load another PySide script/app

Hello, I am writing a PySide6 application which is separated in different python files. I have been trying to import a module and load a class which generate a new window; however, I am receiving the following error: “Please destroy the QApplication singleton before creating a new QApplication instance”.
How could I solve this, and what is the best way to do what I want to accomplish?

@Ruben_Quijada From your question I am going to assume that you are wanting to construct two separate QMainWindow classes. It is also possible to have a separate window using a QDialog class, but dialogs are typically intended for modal operations to present or gather information. I haven’t replicated your error message, but it seems like you are trying to create two instances of QApplication which isn’t needed.

I’ll give you two separate options that you can use as a base for your application. I also recommend that you look at the article right here on PythonGUIs: Multiple windows in PyQt6 My options are slightly different that what is posted there, but they all should work as a foundation of what you’re trying to do, just pick the one that’ll work best for your situation.

Option 1 - Launch both windows at the same time

window_one.py

from PySide6.QtWidgets import QMainWindow, QLabel

# Define the class for the first window
class MainWindowOne(QMainWindow):
def __init__(self):
    super().__init__()
    self.setCentralWidget(QLabel("Window One"))

# Launch the application when this file is ran
if __name__ == "__main__":
    import sys
    from PySide6.QtWidgets import QApplication

    # import the QMainWindow class from window_two.py module
    from window_two import MainWindowTwo

    # Instantiate a single QApplication
    app = QApplication(sys.argv) 

    # Instantiate both windows
    window_one = MainWindowOne() 
    window_two = MainWindowTwo()

    # Show both windows
    window_one.show()
    window_two.show()

    # Launch the application
    sys.exit(app.exec())

window_two.py

from PySide6.QtWidgets import QMainWindow, QLabel

class MainWindowTwo(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setCentralWidget(QLabel("Window Two"))

Option 2 - Launch window two from window one

window_one.py

from PySide6.QtWidgets import QMainWindow, QLabel, QPushButton, QVBoxLayout, QWidget
from window_two import MainWindowTwo

class MainWindowOne(QMainWindow):
    def __init__(self):
        super().__init__()
        layout = QVBoxLayout()
        layout.addWidget(QLabel("Window One"))
        button = QPushButton("Launch Window Two")
        button.clicked.connect(self._launch_window_two)
        layout.addWidget(button)
        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

    def _launch_window_two(self):
        self.window_two = MainWindowTwo()
        self.window_two.show()

if __name__ == "__main__":
    import sys
    from PySide6.QtWidgets import QApplication

    # Instantiate a single QApplication
    app = QApplication(sys.argv)

    # Instantiate a single QMainWindow and show it
    window_one = MainWindowOne()
    window_one.show()

    # Launch the application
    sys.exit(app.exec())

window_two.py

from PySide6.QtWidgets import QMainWindow, QLabel

class MainWindowTwo(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setCentralWidget(QLabel("Window Two"))
1 Like