将 Qthread 的 bat 文件输出更新为 Gui
Update bat file output from Qthread to Gui
我是一名系统管理员,这是我第一次尝试使用 Python 来实现某些目标。我正在开发一个小型 python 工具,它将 运行 Qthread 中的 bat 文件。在 GUI 上,我有一个文本编辑框,我想在其中从 bat 文件更新 output/error。
这是我目前的代码,
QThread -
class runbat(QtCore.QThread):
line_printed = QtCore.pyqtSignal(str)
def __init__(self, ):
super(runbat, self).__init__()
def run(self):
popen = subprocess.Popen("install.bat", stdout=subprocess.PIPE, shell=True)
lines_iterator = iter(popen.stdout.readline, b"")
for line in lines_iterator:
self.line_printed.emit(line)
来自主要 -
self.batfile.line_printed.connect(self.batout)
def batout(self, line):
cursor = self.ui.textEdit.textCursor()
cursor.movePosition(cursor.End)
cursor.insertText(line)
self.ui.textEdit.ensureCursorVisible()
但我收到了 - TypeError: runbat.line_printed[str].emit(): argument 1 has unexpected type 'bytes'。另一个问题是 stdout 是捕获错误还是只是输出,我还需要什么来捕获错误?
好的,我可以通过将代码更改为以下内容来让它工作。
在 Qthread 中
line_printed = QtCore.pyqtSignal(bytes)
主要
def batout(self, line):
output = str(line, encoding='utf-8')
cursor = self.ui.textEdit.textCursor()
cursor.movePosition(cursor.End)
cursor.insertText(output)
self.ui.textEdit.ensureCursorVisible()
基本上输出是以字节为单位的,我不得不将它转换成字符串。它按预期工作,但如果有人有更好的解决方案,我很乐意尝试。谢谢大家
我是一名系统管理员,这是我第一次尝试使用 Python 来实现某些目标。我正在开发一个小型 python 工具,它将 运行 Qthread 中的 bat 文件。在 GUI 上,我有一个文本编辑框,我想在其中从 bat 文件更新 output/error。
这是我目前的代码,
QThread -
class runbat(QtCore.QThread):
line_printed = QtCore.pyqtSignal(str)
def __init__(self, ):
super(runbat, self).__init__()
def run(self):
popen = subprocess.Popen("install.bat", stdout=subprocess.PIPE, shell=True)
lines_iterator = iter(popen.stdout.readline, b"")
for line in lines_iterator:
self.line_printed.emit(line)
来自主要 -
self.batfile.line_printed.connect(self.batout)
def batout(self, line):
cursor = self.ui.textEdit.textCursor()
cursor.movePosition(cursor.End)
cursor.insertText(line)
self.ui.textEdit.ensureCursorVisible()
但我收到了 - TypeError: runbat.line_printed[str].emit(): argument 1 has unexpected type 'bytes'。另一个问题是 stdout 是捕获错误还是只是输出,我还需要什么来捕获错误?
好的,我可以通过将代码更改为以下内容来让它工作。
在 Qthread 中
line_printed = QtCore.pyqtSignal(bytes)
主要
def batout(self, line):
output = str(line, encoding='utf-8')
cursor = self.ui.textEdit.textCursor()
cursor.movePosition(cursor.End)
cursor.insertText(output)
self.ui.textEdit.ensureCursorVisible()
基本上输出是以字节为单位的,我不得不将它转换成字符串。它按预期工作,但如果有人有更好的解决方案,我很乐意尝试。谢谢大家