简体   繁体   中英

How can I execute my script and print it in the GUI in PYQT5?

I am not sure if the path to the practice2.py script is correct. How would I format the path

@QtCore.pyqtSlot()
    def on_pushButtonPrint_clicked(self):
        cmd = '**user/hugo caldeira/desktop/python/practice2.py**' 

This is the path that I would normally use to execute the script through the terminal.

This is the error I keep getting

users: extra operand ‘caldeira/desktop/python/practice2.py’
Try 'users --help' for more information.
Traceback (most recent call last):
  File "windowWaction.py", line 36, in on_pushButtonPrint_clicked
    output = subprocess.check_output(cmd, shell=True)
  File "C:\Users\Hugo Caldeira\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 336, in check_output
    **kwargs).stdout
  File "C:\Users\Hugo Caldeira\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 418, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'users/hugo caldeira/desktop/python/practice2.py' returned non-zero exit status 1.

import sys
import os
import subprocess
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QWidget, QApplication


class MyStream(QtCore.QObject):
    message = QtCore.pyqtSignal(str)
    def __init__(self, parent=None):
        super(MyStream, self).__init__(parent)

    def write(self, message):
        self.message.emit(str(message))

class MyWindow(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.pushButtonPrint = QtWidgets.QPushButton(self)
        self.pushButtonPrint.setText("Click Me!")

        self.pushButtonPrint.clicked.connect(self.on_pushButtonPrint_clicked)

        self.textEdit = QtWidgets.QTextEdit(self)

        self.layoutVertical = QtWidgets.QVBoxLayout(self)
        self.layoutVertical.addWidget(self.pushButtonPrint)
        self.layoutVertical.addWidget(self.textEdit)

    @QtCore.pyqtSlot()
    def on_pushButtonPrint_clicked(self):
        cmd = 'user/hugo caldeira/desktop/python/practice2.py'

        # execute script
        output = subprocess.check_output(cmd, shell=True)
        print (output)

    @QtCore.pyqtSlot(str)
    def on_myStream_message(self, message):
        self.textEdit.moveCursor(QtGui.QTextCursor.End)
        self.textEdit.insertPlainText(message)

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()

    myStream = MyStream()
    myStream.message.connect(main.on_myStream_message)

    sys.stdout = myStream
    sys.exit(app.exec_())

Try it:

import sys
import os
import subprocess
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QWidget, QApplication


class MyStream(QtCore.QObject):
    message = QtCore.pyqtSignal(str)
    def __init__(self, parent=None):
        super(MyStream, self).__init__(parent)

    def write(self, message):
        self.message.emit(str(message))

class MyWindow(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.pushButtonPrint = QtWidgets.QPushButton(self)
        self.pushButtonPrint.setText("Click Me!")

        self.pushButtonPrint.clicked.connect(self.on_pushButtonPrint_clicked)

        self.textEdit = QtWidgets.QTextEdit(self)

        self.layoutVertical = QtWidgets.QVBoxLayout(self)
        self.layoutVertical.addWidget(self.pushButtonPrint)
        self.layoutVertical.addWidget(self.textEdit)

    @QtCore.pyqtSlot()
    def on_pushButtonPrint_clicked(self):
        #cmd = 'user/hugo caldeira/desktop/python/practice2.py'
        cmd = 'python file_import.py'                                      # +++ `python `

        # execute script
        #output = subprocess.check_output(cmd, shell=True)                 # ---
        output = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) # +++
        print (output)

    @QtCore.pyqtSlot(str)
    def on_myStream_message(self, message):
        self.textEdit.moveCursor(QtGui.QTextCursor.End)
        self.textEdit.insertPlainText(message)

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()

    myStream = MyStream()
    myStream.message.connect(main.on_myStream_message)

    sys.stdout = myStream
    sys.exit(app.exec_())

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM