如何将终端输出显示到 PyQt 中的文本框
Posted
技术标签:
【中文标题】如何将终端输出显示到 PyQt 中的文本框【英文标题】:How can I display terminal output to a text box in PyQt 【发布时间】:2019-08-19 19:46:43 【问题描述】:我正在编写一个 GUI,它具有将输出打印到终端的功能。我想让它显示在某种文本框或类似的东西中。
代码如下:
def radial_distance(body1, body2, utc, ref, abcorr, obs):
x_1 = 1
x_2 = 4
y_1 = 5
y_2 = 2
z_1 = 7
z_2 = 6
d_rad = np.sqrt((x_2 - x_1)**2.0 + (y_2 - y_1)**2.0 + (z_2 - z_1)**2.0)
return d_rad
我尝试了文本标签,但我需要更多空间来打印(需要向下滚动)。还尝试了文本浏览器和文本编辑,但没有找到显示方式。
PyQt 的样子:
from Distances import radial_distance
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_window1(object):
def setupUi(self, window1):
window1.setObjectName("window1")
window1.resize(485, 530) # 820 530
self.centralwidget = QtWidgets.QWidget(window1)
self.centralwidget.setObjectName("centralwidget")
self.output_rd = QtWidgets.QTextBrowser(self.groupBox_2)
self.output_rd.setGeometry(QtCore.QRect(10, 90, 331, 111))
self.output_rd.setObjectName("output_rd")
def retranslateUi(self, window1):
_translate = QtCore.QCoreApplication.translate
window1.setWindowTitle(_translate("window1", "GUI"))
def rad_distance(self):
time_rd = np.asarray(self.get_time_rd())
body1, body2 = ['EARTH', 'SUN']
rad_dis = radial_distance(body1, body2, time_rd, 'HCI', 'NONE', 'SUN')
self.output_rd.setText(rad_dis) # This doesnt work
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window1 = QtWidgets.QMainWindow()
ui = Ui_window1()
ui.setupUi(window1)
window1.show()
sys.exit(app.exec_())
你能帮帮我吗?谢谢!
【问题讨论】:
添加你已有的代码 @RyanSchaefer 就是这样。我也尝试返回 d_rad 并用 self.label.setText(d_rad) 调用它,但没有用:( @S.Nick 我不知道这是不是你想要的,因为我有很多功能,但我想它总结了一切 【参考方案1】:试试看:
from PyQt5 import QtCore, QtGui, QtWidgets
import numpy as np
#from Distances import radial_distance
def radial_distance(body1, body2, utc, ref, abcorr, obs):
x_1 = 1
x_2 = 4
y_1 = 5
y_2 = 2
z_1 = 7
z_2 = 6
d_rad = np.sqrt((x_2 - x_1)**2.0 + (y_2 - y_1)**2.0 + (z_2 - z_1)**2.0)
return d_rad
class Ui_window1(object):
def setupUi(self, window1):
window1.setObjectName("window1")
window1.resize(485, 530) # 820 530
self.centralwidget = QtWidgets.QWidget(window1)
self.centralwidget.setObjectName("centralwidget")
window1.setCentralWidget(self.centralwidget)
self.groupBox_2 = QtWidgets.QGroupBox("groupBox_2", self.centralwidget)
self.output_rd = QtWidgets.QTextBrowser(self.groupBox_2)
self.output_rd.setGeometry(QtCore.QRect(10, 90, 331, 111))
self.output_rd.setObjectName("output_rd")
self.retranslateUi(window1)
QtCore.QMetaObject.connectSlotsByName(window1)
def retranslateUi(self, window1):
_translate = QtCore.QCoreApplication.translate
window1.setWindowTitle(_translate("window1", "GUI"))
def rad_distance(self):
time_rd = np.asarray([1, 2]) # ? (self.get_time_rd())
body1, body2 = ['EARTH', 'SUN']
rad_dis = radial_distance(body1, body2, time_rd, 'HCI', 'NONE', 'SUN')
# self.output_rd.setText(rad_dis)
self.output_rd.append(str(rad_dis)) # + str
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window1 = QtWidgets.QMainWindow()
ui = Ui_window1()
ui.setupUi(window1)
ui.rad_distance() # +
window1.show()
sys.exit(app.exec_())
【讨论】:
非常感谢,这行得通。有没有办法在字符串中添加更多字符? (例如公里等单位) @mrcfm4self.output_rd.append(" <-> ".format(str(rad_dis), "kilometers"))
你是我所经历过的最接近上帝的东西,非常感谢你(如果你想出一种插入 linejump 的方法 \n 或任何我保证我会永远爱你的方法)跨度>
@mrcfm4 self.output_rd.append(""" <-> \n or whatever i promise i'll love you forever \n )""".format( str(rad_dis), "kilometers", "Hello World!") )
你是全宇宙最聪明的人,非常非常感谢以上是关于如何将终端输出显示到 PyQt 中的文本框的主要内容,如果未能解决你的问题,请参考以下文章
pyqt5中添加一个按钮b,b的功能是添加一个文本框,添加之后如何显示到界面上?