[英]How to redirect the terminal output of a python CLI program, to non-editable "QTextEdit" inside PyQt5 GUI app?
我是PyQt5的新手。 我有一個用 python(“ ping.py ”)編寫的 CLI 應用程序,它將 ping 結果打印到終端。 我想從另一個 PyQt5 GUI 應用程序(“ test.py ”)調用它,以便 ping.py 的終端output不打印到終端,而是在 Z8BDB75FF7B8CpyD600ACD789FEE.3 的不可編輯QTextEdit中打印。 ,並行和實時(重定向) ,就像在終端上一樣。 任何幫助深表感謝。 相關代碼如下。
平.py :
import os
response = os.popen("ping www.google.com")
for j in response:
print(j)
測試.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_())
您可以將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
然后您可以將ping.py導入test.py模塊並像這樣調用它:
import ping
x = ping.test_connection()
print(x)
結果將是這樣的:
['\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']
注意結果是一個數組,因為我設置了它,但你可以返回任何你想要的......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.