为什么第二个脚本 运行 从另一个开始时没有

Why doesn't second script run when started from another

我在 PyQt5 中创建了一个 Python GUI,它运行良好。 我想在屏幕上显示 GUI 后在后台启动另一个 python 脚本到 运行。我试过导入另一个 python 文件并启动它,但没有成功。我还尝试使用 os.system() 命令执行另一个 python 脚本,但也没有用。

当我 运行 我发布的代码时,gui window 启动得很好。 但是在 GUI 启动后我需要 运行 的脚本根本不会出现 运行。

我怎样才能正确地运行?

这是我的代码:

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

调用app.exec_()后,在应用程序退出之前不会执行任何后续代码。因此,一种解决方案是在 event-loop 启动后使用 single-shot 计时器执行函数:

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_())