gpt4 book ai didi

python - 如何执行我的脚本并在 PYQT5 的 GUI 中打印它?

转载 作者:行者123 更新时间:2023-12-01 08:55:48 31 4
gpt4 key购买 nike

我不确定practice2.py脚本的路径是否正确。我将如何格式化路径

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

这是我通常用来通过终端执行脚本的路径。

这是我不断收到的错误

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

最佳答案

尝试一下:

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

enter image description here

关于python - 如何执行我的脚本并在 PYQT5 的 GUI 中打印它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52779634/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com