[英]How to redirect the terminal output of a python CLI program, to non-editable "QTextEdit" inside PyQt5 GUI app?
I am new to PyQt5 .我是PyQt5的新手。 I have a CLI app written in python (" ping.py "), that prints ping result into the terminal.
我有一个用 python(“ ping.py ”)编写的 CLI 应用程序,它将 ping 结果打印到终端。 I want to call it from another PyQt5 GUI app (" test.py "), so that the terminal output of ping.py is printed not to terminal, but inside non-editable QTextEdit of the PyQt5 GUI (" test.py "), in parallel and in real-time (redirection) , just as it would on the terminal.
我想从另一个 PyQt5 GUI 应用程序(“ test.py ”)调用它,以便 ping.py 的终端output不打印到终端,而是在 Z8BDB75FF7B8CpyD600ACD789FEE.3 的不可编辑QTextEdit中打印。 ,并行和实时(重定向) ,就像在终端上一样。 Any help is much appreciated.
任何帮助深表感谢。 Relevant codes are given below.
相关代码如下。
ping.py :平.py :
import os
response = os.popen("ping www.google.com")
for j in response:
print(j)
test.py :测试.py :
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(285, 445)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(10, 10, 261, 391))
self.textEdit.setReadOnly(True)
self.textEdit.setObjectName("textEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 285, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.textEdit.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Hello</p></body></html>"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
You could change ping.py to have a function that returns the results of the ping:您可以将ping.py更改为具有返回 ping 结果的 function:
import os
def test_connection():
response = os.popen("ping www.google.com")
k = []
for j in response:
k.append(j)
return k
And then you can import ping.py into the test.py module and call it like this:然后您可以将ping.py导入test.py模块并像这样调用它:
import ping
x = ping.test_connection()
print(x)
The result would be this:结果将是这样的:
['\n', 'Pinging www.google.com [2a00:1450:4009:817::2004] with 32 bytes of data:\n', 'Reply from 2a00:1450:4009:817::2004: time=25ms \n', 'Reply from 2a00:1450:4009:817::2004: time=10ms \n', 'Reply from 2a00:1450:4009:817::2004: time=10ms \n', 'Reply from 2a00:1450:4009:817::2004: time=12ms \n', '\n', 'Ping statistics for 2a00:1450:4009:817::2004:\n', ' Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),\n', 'Approximate round trip times in milli-seconds:\n', ' Minimum = 10ms, Maximum = 25ms, Average = 14ms\n']
Note the result is an array because i set that, but you could return whatever you wanted...注意结果是一个数组,因为我设置了它,但你可以返回任何你想要的......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.