0

I wrote a script (test.py) for Data Analysis. Now I'm doing a GUI in PyQt. What I want is when I press a button 'Run', the script test.py will run and show the results (plots).

I tried subprocess.call('test1.py') and subprocess.Popen('test1.py') but it only opens the script and don't run it. I also tried os.system, doesn't work either.

The script below is not complete (there are more buttons and functions associated but is not relevant and aren't connect to the problem described).

I'm using Python 3.6 on Spyder and PyQt5.

Is there any other function or module that can do what I want?

class Window(QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("TEMP FILE")

        self.home()

    def home (self):
        btn_run = QPushButton("Run", self)
        btn_run.clicked.connect(self.execute)

        self.show()

    def execute(self):
        subprocess.Popen('test1.py', shell=True)
        subprocess.call(["python", "test1.py"])

if not QtWidgets.QApplication.instance():
    app = QtWidgets.QApplication(sys.argv)
else:
    app = QtWidgets.QApplication.instance()

GUI = Window()
app.exec_()
Luís Martins
  • 23
  • 1
  • 6

3 Answers3

0

You can import test1.py and call functions from within it whenever you wish

Use this How can I make one python file run another?

Ran Elgiser
  • 66
  • 1
  • 1
  • 14
0

What you need to do is create a text label, then pipe stdout / stderr to subprocess.PIPE:

p = subprocess.Popen(
    "python test1.py",
    shell=True,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
)

Then call subprocess.Popen.communicate():

stdout, stderr = p.communicate()
# Display stdout (and possibly stderr) in a text label
Daniel
  • 769
  • 8
  • 21
  • Still not working, probably I have some software issue, because if I paste the code directly in the GUI script it will run perfectly. – Luís Martins Apr 23 '18 at 07:17
0

QProcess class is used to start external programs and to communicate with them.

Try it:

import sys
import subprocess

from PyQt5 import Qt
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton
from PyQt5.QtCore import QProcess

class Window(QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("TEMP FILE")
        self.home()

    def home (self):
        btn_run = QPushButton("Run", self)
        #btn_run.clicked.connect(self.execute)                                   # ---
        filepath = "python test1.py"                                             # +++
        btn_run.clicked.connect(lambda checked, arg=filepath: self.execute(arg)) # +++

        self.show()

    def execute(self, filepath):                                         # +++
        #subprocess.Popen('test1.py', shell=True)
        #subprocess.call(["python", "test1.py"])

        # It works
        #subprocess.run("python test1.py")

        QProcess.startDetached(filepath)                                 # +++



if not QApplication.instance():
    app = QApplication(sys.argv)
else:
    app = QApplication.instance()

GUI = Window()
app.exec_()

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33
  • Still not working, probably I have some software issue, because if I paste the code directly in the GUI script it will run perfectly. – Luís Martins Apr 23 '18 at 07:16