简体   繁体   中英

How to redirect the terminal output of a python CLI program, to non-editable "QTextEdit" inside PyQt5 GUI app?

I am new to PyQt5 . I have a CLI app written in python (" ping.py "), that prints ping result into the terminal. 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. Any help is much appreciated. Relevant codes are given below.

ping.py :

import os

response = os.popen("ping www.google.com")

for j in response:
    print(j)

test.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:

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:

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...

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