如何在不点击任何按钮的情况下将 Python 脚本输出重定向到 PyQT5 GUI 控制台?
How to redirect Python script output to PyQT5 GUI Console, without any buttons clicks?
嘿,我使用相同的旧代码来获取 PyQT5 QTexBrowser GUI 控制台的所有代码输出,但我也在所有新行中获取下一行字符(“\n”)。
代码 URL - pipe the output of sys.stdout to text browser in pyqt
下面是我在上面使用的代码 URL -
class Port(object):
def __init__(self, view):
self.view = view
def write(self, *args):
# self.view.append(str([*args]))
# self.view.append(str(*args).rstrip("\n").lstrip("\n"))
self.view.append(*args)
GUI 控制台输出 -
有什么方法可以从 args 中删除 "\n"
,请提出建议。
当我使用下面的行时,我能够看到每个新参数中都有 "\n"
。
self.view.append(str([*args]))
带有可见 "\n"
的 GUI 控制台输出(*忽略作为第一个字符的行中的数字 - 这是用户选择输出的一部分。)
然后我尝试了所有可能的方法来删除像 strip(), lstrip() 等,仍然没有成功。
下面是代码的一部分,我如何将 运行 时间输出重定向到 QtextBrowser
self.execution_result = QtWidgets.QWidget()
self.execution_result.setObjectName("execution_result")
self.execution_rslt_saveresult_btn = QtWidgets.QPushButton(self.execution_result)
self.execution_rslt_saveresult_btn.setGeometry(QtCore.QRect(1220, 780, 131, 25))
self.execution_rslt_saveresult_btn.setObjectName("execution_rslt_saveresult_btn")
# ------------------------------------------------------------------------------------------------------#
# Console Output
# ------------------------------------------------------------------------------------------------------#
self.execution_rslt_textbrowser = QtWidgets.QTextBrowser(self.execution_result)
self.execution_rslt_textbrowser.setGeometry(QtCore.QRect(2, 1, 1371, 771))
self.execution_rslt_textbrowser.setObjectName("execution_rslt_textbrowser")
self.execution_rslt_textbrowser.setStyleSheet(
"""QTextBrowser {background-color: #000000;
color: #00FF00;
font: 11pt Courier new;}""")
self.execution_rslt_textbrowser.document().setPlainText(
"-" * 40 + '\n' + " Log starts from below line " + '\n' + "-" * 40)
# ------------------------------------------------------------------------------------------------------#
# Below line is sending run time output to GUI console
# ------------------------------------------------------------------------------------------------------#
sys.stdout = Port(self.execution_rslt_textbrowser)
请任何人建议我应该在此处做什么以从输出行中删除 "\n"
。
还有如何删除错误 AttributeError: 'port' object has no attribute 'flush'
下面是带有下一行字符问题的工作示例代码。 - reference/debugging
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Port(object):
def __init__(self, view):
self.view = view
def flush(self):
pass
def write(self, *args):
# self.view.append(str([*args])) # This line will highlight next line character in console "\n"
self.view.append(*args)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
# MainWindow.setEnabled(True)
MainWindow.resize(1000, 500)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setMinimumSize(QtCore.QSize(1000, 500))
MainWindow.setMaximumSize(QtCore.QSize(1000, 500))
MainWindow.setBaseSize(QtCore.QSize(1000, 500))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setMinimumSize(QtCore.QSize(0, 883))
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(280, 0, 671, 341))
self.tabWidget.setTabPosition(QtWidgets.QTabWidget.North)
self.tabWidget.setTabShape(QtWidgets.QTabWidget.Rounded)
self.tabWidget.setDocumentMode(False)
self.tabWidget.setTabsClosable(False)
self.tabWidget.setObjectName("tabWidget")
self.execution_result = QtWidgets.QWidget()
self.execution_result.setObjectName("execution_result")
self.execution_rslt_textbrowser = QtWidgets.QTextBrowser(self.execution_result)
self.execution_rslt_textbrowser.setGeometry(QtCore.QRect(1, 1, 681, 321))
self.execution_rslt_textbrowser.setObjectName("execution_rslt_textbrowser")
self.execution_rslt_textbrowser.document().setPlainText(
"-" * 40 + '\n' + " Log starts from below line " + '\n' + "-" * 40)
# ----------------------------------------------------------------------------#
# Console Output Redirection
# ----------------------------------------------------------------------------#
sys.stdout = Port(self.execution_rslt_textbrowser)
# ----------------------------------------------------------------------------#
self.tabWidget.addTab(self.execution_result, "")
self.mainwindow_list = QtWidgets.QListWidget(self.centralwidget)
self.mainwindow_list.setGeometry(QtCore.QRect(20, 30, 241, 301))
self.mainwindow_list.setObjectName("mainwindow_list")
self.mainwindow_list.itemClicked['QListWidgetItem*'].connect(self.qlistoptions)
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setPointSize(9)
item.setFont(font)
self.mainwindow_list.addItem(item)
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setPointSize(9)
item.setFont(font)
self.mainwindow_list.addItem(item)
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setPointSize(9)
item.setFont(font)
self.mainwindow_list.addItem(item)
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setPointSize(9)
item.setFont(font)
self.mainwindow_list.addItem(item)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.execution_result), _translate("MainWindow", "Execution Console"))
__sortingEnabled = self.mainwindow_list.isSortingEnabled()
self.mainwindow_list.setSortingEnabled(False)
item = self.mainwindow_list.item(0)
item.setText(_translate("MainWindow", "Item1"))
item = self.mainwindow_list.item(1)
item.setText(_translate("MainWindow", "Item2"))
item = self.mainwindow_list.item(2)
item.setText(_translate("MainWindow", "Item3"))
item = self.mainwindow_list.item(3)
item.setText(_translate("MainWindow", "Item4"))
self.mainwindow_list.setSortingEnabled(__sortingEnabled)
def qlistoptions(self):
print("Inside qlistoptions method : Button Clicked")
# window.hide() ## This to close main window and open new window.
print(self.mainwindow_list.currentRow()) # Will Return Index number of list item
print(self.mainwindow_list.currentItem().text()) # Will Return current selected text from list item
# print("Signal-", self.mainwindow_ok.isChecked())
# Item1
if self.mainwindow_list.currentRow() == 0:
print("Item1 is Clicked!!")
# Item2
elif self.mainwindow_list.currentRow() == 1:
print("Item2 is Clicked!!")
# Item3
elif self.mainwindow_list.currentRow() == 2:
print("Item3 is Clicked!!")
# Item4
elif self.mainwindow_list.currentRow() == 3:
print("Item4 is Clicked!!")
else:
print("Please select any item first!!")
def printText(self, button):
print("Value from printText Method -")
print(button.text())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我用命令写的:sys.stdout.write("Append a line to console!")
,一切都很好,没有多余的。
要删除 flush 属性错误,只需将此覆盖功能添加到您的端口 class:
def flush(self):
pass
已测试!
append方法增加了一个额外的endline,解决方法是插入文本而不添加:
class Port(object):
def __init__(self, view):
self.view = view
def flush(self):
pass
def write(self, text):
cursor = self.view.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
cursor.insertText(text)
self.view.setTextCursor(cursor)
self.view.ensureCursorVisible()
嘿,我使用相同的旧代码来获取 PyQT5 QTexBrowser GUI 控制台的所有代码输出,但我也在所有新行中获取下一行字符(“\n”)。
代码 URL - pipe the output of sys.stdout to text browser in pyqt
下面是我在上面使用的代码 URL -
class Port(object):
def __init__(self, view):
self.view = view
def write(self, *args):
# self.view.append(str([*args]))
# self.view.append(str(*args).rstrip("\n").lstrip("\n"))
self.view.append(*args)
GUI 控制台输出 -
有什么方法可以从 args 中删除 "\n"
,请提出建议。
当我使用下面的行时,我能够看到每个新参数中都有 "\n"
。
self.view.append(str([*args]))
带有可见 "\n"
的 GUI 控制台输出(*忽略作为第一个字符的行中的数字 - 这是用户选择输出的一部分。)
然后我尝试了所有可能的方法来删除像 strip(), lstrip() 等,仍然没有成功。
下面是代码的一部分,我如何将 运行 时间输出重定向到 QtextBrowser
self.execution_result = QtWidgets.QWidget()
self.execution_result.setObjectName("execution_result")
self.execution_rslt_saveresult_btn = QtWidgets.QPushButton(self.execution_result)
self.execution_rslt_saveresult_btn.setGeometry(QtCore.QRect(1220, 780, 131, 25))
self.execution_rslt_saveresult_btn.setObjectName("execution_rslt_saveresult_btn")
# ------------------------------------------------------------------------------------------------------#
# Console Output
# ------------------------------------------------------------------------------------------------------#
self.execution_rslt_textbrowser = QtWidgets.QTextBrowser(self.execution_result)
self.execution_rslt_textbrowser.setGeometry(QtCore.QRect(2, 1, 1371, 771))
self.execution_rslt_textbrowser.setObjectName("execution_rslt_textbrowser")
self.execution_rslt_textbrowser.setStyleSheet(
"""QTextBrowser {background-color: #000000;
color: #00FF00;
font: 11pt Courier new;}""")
self.execution_rslt_textbrowser.document().setPlainText(
"-" * 40 + '\n' + " Log starts from below line " + '\n' + "-" * 40)
# ------------------------------------------------------------------------------------------------------#
# Below line is sending run time output to GUI console
# ------------------------------------------------------------------------------------------------------#
sys.stdout = Port(self.execution_rslt_textbrowser)
请任何人建议我应该在此处做什么以从输出行中删除 "\n"
。
还有如何删除错误 AttributeError: 'port' object has no attribute 'flush'
下面是带有下一行字符问题的工作示例代码。 - reference/debugging
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Port(object):
def __init__(self, view):
self.view = view
def flush(self):
pass
def write(self, *args):
# self.view.append(str([*args])) # This line will highlight next line character in console "\n"
self.view.append(*args)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
# MainWindow.setEnabled(True)
MainWindow.resize(1000, 500)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setMinimumSize(QtCore.QSize(1000, 500))
MainWindow.setMaximumSize(QtCore.QSize(1000, 500))
MainWindow.setBaseSize(QtCore.QSize(1000, 500))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setMinimumSize(QtCore.QSize(0, 883))
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(280, 0, 671, 341))
self.tabWidget.setTabPosition(QtWidgets.QTabWidget.North)
self.tabWidget.setTabShape(QtWidgets.QTabWidget.Rounded)
self.tabWidget.setDocumentMode(False)
self.tabWidget.setTabsClosable(False)
self.tabWidget.setObjectName("tabWidget")
self.execution_result = QtWidgets.QWidget()
self.execution_result.setObjectName("execution_result")
self.execution_rslt_textbrowser = QtWidgets.QTextBrowser(self.execution_result)
self.execution_rslt_textbrowser.setGeometry(QtCore.QRect(1, 1, 681, 321))
self.execution_rslt_textbrowser.setObjectName("execution_rslt_textbrowser")
self.execution_rslt_textbrowser.document().setPlainText(
"-" * 40 + '\n' + " Log starts from below line " + '\n' + "-" * 40)
# ----------------------------------------------------------------------------#
# Console Output Redirection
# ----------------------------------------------------------------------------#
sys.stdout = Port(self.execution_rslt_textbrowser)
# ----------------------------------------------------------------------------#
self.tabWidget.addTab(self.execution_result, "")
self.mainwindow_list = QtWidgets.QListWidget(self.centralwidget)
self.mainwindow_list.setGeometry(QtCore.QRect(20, 30, 241, 301))
self.mainwindow_list.setObjectName("mainwindow_list")
self.mainwindow_list.itemClicked['QListWidgetItem*'].connect(self.qlistoptions)
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setPointSize(9)
item.setFont(font)
self.mainwindow_list.addItem(item)
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setPointSize(9)
item.setFont(font)
self.mainwindow_list.addItem(item)
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setPointSize(9)
item.setFont(font)
self.mainwindow_list.addItem(item)
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setPointSize(9)
item.setFont(font)
self.mainwindow_list.addItem(item)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.execution_result), _translate("MainWindow", "Execution Console"))
__sortingEnabled = self.mainwindow_list.isSortingEnabled()
self.mainwindow_list.setSortingEnabled(False)
item = self.mainwindow_list.item(0)
item.setText(_translate("MainWindow", "Item1"))
item = self.mainwindow_list.item(1)
item.setText(_translate("MainWindow", "Item2"))
item = self.mainwindow_list.item(2)
item.setText(_translate("MainWindow", "Item3"))
item = self.mainwindow_list.item(3)
item.setText(_translate("MainWindow", "Item4"))
self.mainwindow_list.setSortingEnabled(__sortingEnabled)
def qlistoptions(self):
print("Inside qlistoptions method : Button Clicked")
# window.hide() ## This to close main window and open new window.
print(self.mainwindow_list.currentRow()) # Will Return Index number of list item
print(self.mainwindow_list.currentItem().text()) # Will Return current selected text from list item
# print("Signal-", self.mainwindow_ok.isChecked())
# Item1
if self.mainwindow_list.currentRow() == 0:
print("Item1 is Clicked!!")
# Item2
elif self.mainwindow_list.currentRow() == 1:
print("Item2 is Clicked!!")
# Item3
elif self.mainwindow_list.currentRow() == 2:
print("Item3 is Clicked!!")
# Item4
elif self.mainwindow_list.currentRow() == 3:
print("Item4 is Clicked!!")
else:
print("Please select any item first!!")
def printText(self, button):
print("Value from printText Method -")
print(button.text())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我用命令写的:sys.stdout.write("Append a line to console!")
,一切都很好,没有多余的。
要删除 flush 属性错误,只需将此覆盖功能添加到您的端口 class:
def flush(self):
pass
已测试!
append方法增加了一个额外的endline,解决方法是插入文本而不添加:
class Port(object):
def __init__(self, view):
self.view = view
def flush(self):
pass
def write(self, text):
cursor = self.view.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
cursor.insertText(text)
self.view.setTextCursor(cursor)
self.view.ensureCursorVisible()