ironmantis7x
ironmantis7x

Reputation: 827

Why doesn't second script run when started from another

I have created a Python GUI in PyQt5 and it works fine. I want to start another python script to run in the background after the GUI is up on the screen. I have tried importing the other python file and starting it, but that has not worked. I have also tried executing the other python script with an os.system() command, and that has not worked either.

When I run the code i posted, the gui window starts up just fine. But the script I need to run after the GUI has started does not appear to run at all.

How can I get this to run correctly?

Here is my code:

import time

from PyQt5.QtWidgets import *
import os
import sys
import jasmineAI_02

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.centralwidget = QWidget()
        self.setCentralWidget(self.centralwidget)

        # self.pushButton1 = QPushButton("Button 1", self.centralwidget)
        # self.pushButton2 = QPushButton("Button 2", self.centralwidget)

        lay = QHBoxLayout(self.centralwidget)
        # lay.addWidget(self.pushButton1)
        # lay.addWidget(self.pushButton2)


stylesheet = """
    MainWindow {
        background-image: url("/home/ironmantis7x/PycharmProjects/JasmineAI_v2/fauxBG.png"); 
        background-repeat: no-repeat; 
        background-position: center;
        background-color: black;
    }
"""

if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setStyleSheet(stylesheet)     # <---
    window = MainWindow()
    window.resize(1024, 600)
    window.show()
    sys.exit(app.exec_())

time.sleep(5)
os.system("python3 /home/ironmantis7x/PycharmProjects/JasmineAI_v2/jasmineAI_02.py")

Upvotes: 1

Views: 48

Answers (1)

ekhumoro
ekhumoro

Reputation: 120798

After calling app.exec_(), any following code won't be executed until the application quits. So one solution is to use a single-shot timer to execute a function after the event-loop is started:

if __name__ == "__main__":
    ...
    window.show()
    # execute function one second after event-processing starts
    QtCore.QTimer.singleShot(1000, lambda: os.system("python3 /home/ironmantis7x/PycharmProjects/JasmineAI_v2/jasmineAI_02.py"))
    sys.exit(app.exec_())

Upvotes: 1

Related Questions